Given this function :
function testFn(a1, a2) {
if (a1)
if (a2)
return 'a1 && a2';
else
return 'else';
}
you would think that the following is true :
testFn(true, true); // expected to return `'a1 && a2'`
testFn(true, false); // expected to return `undefined`
testFn(false, false); // expected to return `'else'`
but no! JavaScript handles the else
as a sibling of the inner if
, not the outer one. If you run the code above here's what you'll get instead :
testFn(true, true); // returns `'a1 && a2'` (as expected)
testFn(true, false); // returns `'else'` (!!)
testFn(false, false); // returns `undefined` (!!)
Beware of this as this can create subtle bugs in your code.