Code Structure
Code Structure
SEMICOLONS:
A semicolon may be omitted in most cases when a line break exists, although it is
good practice to include them anyway.
In the case of no semicolons, JavaScript will interpret the line break as an
"implicit" semicolon. This is called automatic semicolon
insertion.
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:
alert(3 +
1
+ 2);
The code outputs 6 because JavaScript does not insert semicolons here. It is
intuitively obvious that if the line
ends with a plus ("+"), then it is an "incomplete expression", so a semicolon there
would be incorrect. And in this
case, that works as intended.
COMMENTS:
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.
MULTILINE COMMENTS START WITH A FORWARD SLASH AND AN ASTERISK (/*) AND END WITH AN
ASTERISK AND FORWARD
SLASH (*/).
The content of comments is ignored, so if we put code inside /*...*/ it won't
execute.
Comments increase the overall code footprint, but that's not a problem at all.
There are many tools which minify
code before publishing to a production server. They remove comments, so they don't
appear in the working
scripts. Therefore, comments do not have negative effects on production at all.