Code structure in JavaScript: Semicolon and Comment

Last Updated :

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' );
Comment
Next Article
Mithlesh Upadhyay Published 25 Jun, 2023 · 3 min read

Mithlesh Upadhyay is a Computer Science and AI expert from Madhya Pradesh with strong academic background (BE in CSE and M.Tech in AI) and over six years of experience in technical content development. He has contributed tech articles, led teams, and worked in Full Stack Development and Data Science. He founded the w3colleges.org portal for learning resources.

Article Tags :

Similar Reads

  • Strict mode in JavaScript

    For a long time, JavaScript had no compatibility issues because in every new version release old functionality were not changing. But it was not good as if there…

    2 min read
  • Hello, world! in JavaScript

    In this part of the tutorial, we will learn on core JavaScript. We need working environment o run our scripts. We will use either IDE or browser to…

    4 min read
  • Developer console in web browsers

    Code is prone to errors, and as a human, it’s natural to make mistakes. In the browser, errors are not visible to users by default, which makes it…

    2 min read
  • JavaScript Manuals and specifications

    This is a tutorial designed to help you learn the language gradually, but you’ll eventually need additional resources. 1. Specification The ECMA-262 specification is the most detailed and…

    2 min read
  • Code editors

    Code editors are where programmers spend most of their time. There are two main types of code editors: IDEs and Lightweight editors. IDEs (Integrated Development Environments) IDEs (Integrated…

    1 min read
  • An Introduction to JavaScript

    JavaScript was initially created to “make web pages alive”. These JavaScript program are called as scripts. These can be written in HTML of web page and will simply…

    4 min read