return on its own line returns undefined
JavaScript inserts a semicolon after a return that ends its line, so the function returns undefined and the value below is never reached.
JavaScriptNull and undefined errors
What it means
Automatic semicolon insertion (ASI) treats return followed by a line break as return;. The object or expression on the next line becomes unreachable code. Linters flag it; the language does not.
Common causes
1. Returning an object on the next line
The opening brace must be on the return line.
Breaks
return
{
ok: true
};Works
return {
ok: true
};2. Returning a long expression
Wrap it in parentheses that start on the return line.
Breaks
return
a && b && c;Works
return (
a && b && c
);How to find it in your own code
Never put a line break directly after return. Start the value, a bracket or a parenthesis on the same line, and use a linter (no-unreachable) to catch it.
Still not sure why yours breaks?
Paste it into the visualizer and watch it run line by line, with every variable at every step. Free, and it runs in your browser.
Other common errors
TypeError: Cannot read properties of undefinedTypeError: Cannot read properties of undefined (reading 'map')TypeError: Cannot read properties of undefined (reading 'length')typeof null is "object"AttributeError: 'NoneType' object has no attributeTypeError: unsupported operand type(s) for +: 'int' and 'NoneType'KeyError: 'key' in a Python dictionaryTypeError: 'NoneType' object is not subscriptable