Sub topic 16
The Line Break After return Pitfall
This chapter assumes you know how return works and that a function without a reached return value gives back undefined. It also assumes you can read object literals and arrow functions. It covers one specific trap: a line break placed directly after the return keyword.
What Goes Wrong
JavaScript lets you leave out most semicolons. The parser inserts them for you through a process called automatic semicolon insertion (ASI). For most statements, ASI adds a semicolon only when the next line cannot continue the current statement. return follows a stricter rule. If a line break comes right after return, the parser ends the statement there, every time.
function getConfig() {
return
{
debug: true
};
}
console.log(getConfig()); // undefinedThe parser reads this code as follows:
function getConfig() {
return;
{
debug: true;
}
}The function returns undefined. The braces that follow are not parsed as an object literal. They form a block statement that holds a label called debug and the expression true. That is valid syntax, so you get no error. The code just never runs.
It Is Not Only Objects
The same thing happens with any expression that starts on the next line:
function total(a, b) {
return
a + b;
}
console.log(total(2, 3)); // undefinedThe Fix
Start the returned expression on the same line as return. If the expression is long, put an opening brace or parenthesis on the return line and spread the rest across as many lines as you need.
function getConfig() {
return {
debug: true
};
}
function total(a, b) {
return (
a +
b
);
}
console.log(getConfig()); // { debug: true }
console.log(total(2, 3)); // 5Wrapping a multi-line JSX or arithmetic expression in parentheses is the common way to do this.
Related Restricted Positions
return is one of several "restricted productions," places where the grammar does not allow a line break:
| Construct | Effect of a line break |
|---|---|
| `return` / `yield` | Statement ends, value is lost |
| `break` / `continue` label | Label is ignored |
| `throw` | SyntaxError |
| Postfix `x++` | `++` attaches to the next line |
| Arrow `=>` | SyntaxError if the break comes before `=>` |
Adding semicolons yourself does not protect you here. ASI inserts the semicolon after return no matter what you write at the end of the next line.
Exercise
What does check() return, and why? function check() { return\n ["ok", 200]; } (where \n is a real line break)
Show answer
It returns undefined. The line break right after return makes ASI end the statement there, so ["ok", 200] becomes an unreachable expression statement. To fix it, write return ["ok", 200]; on one line.
Exercise
Rewrite return\n user.firstName + " " + user.lastName; so it spans two lines and still returns the full name.
Show answer
Put an opening parenthesis on the return line: return (\n user.firstName + " " + user.lastName\n);. You could also keep the start of the expression on that line, as in return user.firstName +\n " " + user.lastName;.