0% found this document useful (0 votes)
11 views

Code Structure

Statements are code constructs that perform actions. Statements can be separated with semicolons and written across multiple lines to improve readability. Semicolons can usually be omitted when a line break exists, though it is good practice to include them. There are cases where JavaScript fails to implicitly insert a needed semicolon. Comments are used to describe what code does and why it is complex. Single line comments start with // and multi-line comments start with /* and end with */. Comments do not affect execution but increase code size, though minification removes them before deployment.

Uploaded by

joshuaharris1107
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Code Structure

Statements are code constructs that perform actions. Statements can be separated with semicolons and written across multiple lines to improve readability. Semicolons can usually be omitted when a line break exists, though it is good practice to include them. There are cases where JavaScript fails to implicitly insert a needed semicolon. Comments are used to describe what code does and why it is complex. Single line comments start with // and multi-line comments start with /* and end with */. Comments do not affect execution but increase code size, though minification removes them before deployment.

Uploaded by

joshuaharris1107
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

STATEMENTS:

Statements are syntax constructs and commands that perform actions.


We have already seen a statement, alert(`Hello, World!`), which shows the message
`Hello, World!`.
We can have as many statements in our code as we want. Statements can be separated
with a semi-colon (;).
Usually Statements are written on two separate lines to make the code more
readable.
alert(`Hello`);
alert(`World`);

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.

THERE ARE SITUATIONS WHERE JAVASCRIPT "FAILS" TO ASSUME A SEMICOLON WHERE IT IS


REALLY NEEDED.

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.

NESTED COMMENTS ARE NOT SUPPORTED.

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.

You might also like