Automatic Semicolon Insertion is the cause. When the expression to return starts on a separate line to the return
keyword, a semicolon is inserted after the return
statement. Semicolons are optional in many cases but this is not one of them
So
function returnValue() {
return
"value";
}
ends up becoming/being interpreted as
function returnValue() {
return; // <- semicolon inserted here
"value";
}
which results in undefined
being returned. One way to fix this is to start the value to return on the same line as return
.
There are rules for when automatic semicolon insertion happens, summarized as
The resulting practical advice to ECMAScript programmers is:
- A postfix ++ or -- operator should appear on the same line as its operand.
- An Expression in a return or throw statement should start on the same line as the return or throw token.
- A label in a break or continue statement should be on the same line as the break or continue token.