Statements are syntax constructs and commands that perform actions.
We can have as many statements in our code as we want. Statements can be separated with a semicolon.
For example, here we split “Hello World” into two alerts as shown below:
Usually, statements are written on separate lines to make the code more readable as shown below:
A semicolon may be omitted in most cases when a line break exists.
This would also work:
In most cases, a newline implies a semicolon. But “in most cases” does not mean “always”!
There are cases when a newline does not mean a semicolon. For example:
Errors which occur in such cases are quite hard to find and fix.
An example of an error
If you’re curious to see a concrete example of such an error, check this code out:
No need to think about the meaning of the brackets  and forEach yet. For now, just remember the result of the code: it shows 1 then 2.
Now, let’s add an alert before the code and not finish it with a semicolon:
alert("There will be an error")
Now if we run the code, only the first alert is shown and then we have an error!
But everything is fine again if we add a semicolon after alert:
alert("All fine now");
Now we have the “All fine now” message followed by 1 and 2.
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)
But it should be two separate statements, not one. Such a merging in this case is just wrong, hence the error. This can happen in other situations.
I recommend putting semicolons between statements even if they are separated by newlines. This rule is widely adopted by the community. Let’s note once again – it is possible to leave out semicolons most of the time. But it’s safer – especially for a beginner – to use them.
As time goes on, programs become more and more complex. It becomes necessary to add comments which describe what the code does and why.
Comments can be put into any place of a script. They don’t affect its execution because the engine simply ignores them.
One-line comments start with two forward slash characters //.
The rest of the line is a comment. It may occupy a full line of its own or follow a statement.
// This comment occupies a line of its own
alert('World'); // This comment follows the statement
Multiline comments start with a forward slash and an asterisk /* and end with an asterisk and a forward slash */.
/* An example with two messages.
This is a multiline comment.
The content of comments is ignored, so if we put code inside /* … */, it won’t execute.
Sometimes it can be handy to temporarily disable a part of code:
/* Commenting out the code
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' );