JS Fundamentals - Code Structure Flashcards
Importance of semicolon
alert(“All fine now”);
[1, 2].forEach(alert)
Now we have the “All fine now” message followed by 1 and 2.
The error in the no-semicolon variant occurs because JavaScript does not assume a semicolon before square brackets […].
So, because the semicolon is not auto-inserted, the code in the first example is treated as a single statement. Here’s how the engine sees it:
alert(“There will be an error”)[1, 2].forEach(alert)
Comments
They don’t affect its execution because the engine simply ignores them.
One line comments
One-line comments start with two forward slash characters //.
Multi Line comments
/* An example with two messages. This is a multiline comment. */
HotKeys
In most editors, a line of code can be commented out by pressing the Ctrl+/ hotkey for a single-line comment and something like Ctrl+Shift+/ – for multiline comments (select a piece of code and press the hotkey). For Mac, try Cmd instead of Ctrl and Option instead of Shift.
Nested comments are not supported
There may not be /…/ inside another /…/.
Such code will die with an error:
/* /* nested comment ?!? */ */ alert( 'World' );