In my function, I'm returning the
AND result of several variables, like so:
return // a comment, for kicks a1 && a2 && b1 && b2 && // another comment c1 && c2 && d1 && d2 ;
However, even when all of those variables have an explicit value of
true, the function is returning
undefined instead of the expected
I've tried several variations of returning this expression, and I've found:
See working examples: http://jsfiddle.net/drzaus/38DgX/
Can anybody explain why this happens?
This is correct behavior. Putting
return; in a function will cause it to return
undefined. In your example, the line break after
return makes the parser think this is the end of the statement, so it returns
return statement qualifies -- to prevent this from happening, you could wrap the returned value in parentheses, like so:
return ( a1 && a2 ... d1 && d2 );
There are more details (and a quote from the spec) at: Trouble understanding semicolon insertion
It appears that the parser sees a newline after
return and automatically treats it to a semicolon. In the cases where it works, there's either a parethesis indicating to the parser that it's actually a multiline clause, or it's on a single line (thus negating the autoinsert), or in the case of a variable it's treated differently by the parser.
©2020 All rights reserved.