#86: return followed by a newline and an object
Topic: misc · Difficulty: ★★
function make() {
return
{
value: 42
};
}
console.log(make());Output
undefined
Why
Automatic Semicolon Insertion (ASI) silently inserts a semicolon after return whenever a line break immediately follows the keyword, because return is one of the few statements where the parser refuses to let a token on the next line continue it. So `return` on its own line is really `return;` — the function exits right there with no value, i.e. undefined — and the object literal below is unreachable dead code that never executes. The fix is to keep the opening brace on the same line as return: `return {`. This is exactly why many style guides ban line breaks right after return.