Code structure in JavaScript: Semicolon and Comment

By | June 25, 2023

Semicolons and Comments are buidling blocks of code.

Semicolons

We can have as many statements in our code as we want. Statements can be separated with a semicolon. But, these semicolon are not require everywhere. Engine puts semicolon automatcally after identifying as per its defined rules.

For example, these two statements can be written in same line after inserting semicolon end of each statement.

alert('Hello'); alert('World');

You can also rewrite above in this way (in separate lines) to make the code more readable:

alert('Hello');
alert('World');

And, you can also igonre semicolons at the end of these two statements. Because someotime (but not always) engine puts semicolon end of each statement. This would also work:

alert('Hello')
alert('World')

There will be no semicolons at the end of lines of this code:

alert(5 +
3
+ 2);

This code will work and printed 10.

But there are situations where JavaScript “fails” to assume a semicolon where it is really needed.
If there is no semicolons at the end of line, then Engine appends semicolons at the end of each lines (where there is no semicolons added by programmer). But note that Engine appends semicolons (where there is no semicolons added by programmer) sometimes but not always. So this confusion can create error and inaccurate results.

For example, this code will work fine because of proper semicolons:

alert("Hello");

[1, 2].forEach(alert);

This code shows Hello, then 1, then 2.

But consider this code without a semicolon:

alert("Hello")    // no semicolon here

[1, 2].forEach(alert);

JavaScript does not assume a semicolon before square brackets […]. So, the code in the last example is treated as a single statement:

alert("Hello")[1, 2].forEach(alert);

It will shows a Hello message and an error, you can check browser console:

Hence, we have conclude that we should always use semicolon at the end of each statement in the JavaScript code. It will be safe from producing errors or inaccurate results.

Comments

Comments are usefull in the code. These help to understand the code and its flow. Comments can be put into anywhere in the code. Comments are always ignored and never executed by Engine. Just like in C++ language, JavaScript has also two types of comments.

1. Single Line comments (by using //)

// This is single line comment
alert('Hello');

alert('World'); // This is also a single line comment

2. Multi-Line comments (by using /* … */)

/* This is multiline comment
to shows Hello */
alert('Hello');

/* You can also comment out the code
alert('Hello');
*/

Generally, in windows you can use `Ctrl+/` hotkey for single line to comment. Use `Ctrl+Shift+/` for multiline comments (select a piece of code and press the hotkey).

Note that you can not use nested multi-line comments. There may not be /*…*/ inside another /*…*/. Otherwise it will throw an error.

/*
  /* This is nested comment structure, which will throw error */
*/
alert( 'World' );
Author: Mithlesh Upadhyay

I hold an M.Tech degree in Artificial Intelligence (2023) from Delhi Technological University (DTU) and possess over 4 years of experience. I worked at GeeksforGeeks, leading teams and managing content, including GATE CS, Test Series, Placements, C, and C++. I've also contributed technical content to companies like MarsDev, Tutorialspoint, StudyTonight, TutorialCup, and Guru99. My skill set includes coding, Data Structures and Algorithms (DSA), and Object-Oriented Programming (OOPs). I'm proficient in C++, Python, JavaScript, HTML, CSS, Bootstrap, React.js, Node.js, MongoDB, Django, and Data Science.