Confirming JS Function Return Bug
When returning the results of a large boolean AND operation in a function, it seems that the actual syntax (whitespace) matters.
by drzaus
HTML
<p><em>See stackoverflow question: <a href="http://stackoverflow.com/questions/11972059/does-the-syntax-whitespace-of-a-javascript-function-return-affect-the-result">http://stackoverflow.com/questions/11972059/does-the-syntax-whitespace-of-a-javascript-function-return-affect-the-result</a></em></p>
<p><strong>Update:</strong> turns out it has to do with automatic semicolon insertion by parser.</p>
<pre id="result"></pre>
JavaScript
;(function(){
/// the offending function
function checkBigBlock(a1, a2, b1, b2, c1, c2, d1, d2) {
console.log("fn.checkBigBlock", arguments, a1 && a2 && b1 && b2 && c1 && c2 && d1 && d2);
return
// a comment, for kicks
a1 && a2
&&
b1 && b2
&&
// another comment
c1 && c2
&&
d1 && d2
;
}
/// the offending function, unique variable names?
function checkUniqueVars(a, b, c, d, e, f, g, h) {
console.log("fn.checkUniqueVars", arguments, a && b && c && d && e && f && g && h);
return
// a comment, for kicks
a && b
&&
c && d
&&
// another comment
e && f
&&
g && h
;
}
/// same as offending function, wrapped result in parentheses
function checkWithParentheses(a1, a2, b1, b2, c1, c2, d1, d2) {
console.log("fn.checkWithParentheses", arguments, a1 && a2 && b1 && b2 && c1 && c2 && d1 && d2);
return (
// a comment, for kicks
a1 && a2
&&
b1 && b2
&&
// another comment
c1 && c2
&&
d1 && d2
);
}
/// same as offending function, no inline comments
function checkNoComments(a1, a2, b1, b2, c1, c2, d1, d2) {
console.log("fn.checkNoComments", arguments, a1 && a2 && b1 && b2 && c1 && c2 && d1 && d2);
return
a1 && a2
&&
b1 && b2
&&
c1 && c2
&&
d1 && d2
;
}
/// same as offending function, no newlines
function checkOneLine(a1, a2, b1, b2, c1, c2, d1, d2) {
console.log("fn.checkOneLine", arguments, a1 && a2 && b1 && b2 && c1 && c2 && d1 && d2);
return a1 && a2 && b1...