3 ms·
"As long as the block of code inside the if fits in one line, you can also use this shorthand syntax (note the lack of curly brackets):" My definition of "one
by michaelmcmillan 13y ago
"As long as the block of code inside the if fits in one line, you can also use this shorthand syntax (note the lack of curly brackets):"
My definition of "one line" invalidates this statement. However, I believe "one line" is pretty objective. Javascript will execute anything until it reaches a semi-colon. The following is valid Javascript.
if (foo)
for (i = 0; i < a.length; ++i)
if (i === 5)
console.log (a[i]);
Other than the obvious nitpicking, great article! It clears up a couple of confusing aspects of Javascript.
- igl 13y agoThe thing you end with the semicolon is a expression. if (1) console.log(1), console.log(2), console.log(3); The comma breaks and returns the next value. var foo = 1, 2, 3; foo => 3 So correctly: Use this shorthand syntax to pick up only the next expression. :)