Does the syntax (whitespace) of a javascript function return affect the result?

debugging, javascript, parsing

Solution

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 `undefined`.

Problem

In writing a javascript function to evaluate a multi-variate condition, I came across what seems like a parser error in Javascript. Please let me know if I'm forgetting something, or if this is appropriate behavior. 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 `true`. I've tried several variations of returning this expression, and I've found: - multiline expression -- fails - expression on single line -- works - wrapping expression in paretheses -- works - setting multiline expression to variable, then returning variable -- works See working examples: http://jsfiddle.net/drzaus/38DgX/ Can anybody explain why this happens?

Original source

Related problems