Statements & Comments in JavaScript

In this tutorial post we will understand what are statements & comment in JavaScript.

Statements in JavaScript –

A computer program is a list of “instructions” to be “executed” by a computer. In a programming language, these programming instructions are called statements.  JavaScript program is a list of programming statements.

Most JavaScript programs contain many JavaScript statements. The statements are executed, one by one, in the same order as they are written.

Semicolons (;)

Semicolons separate JavaScript statements. On the web, you might see examples without semicolons.
Ending statements with semicolon is not required, but highly recommended.

var a, b, c;     // Declare 3 variables
a = 5;           // Assign the value 5 to a
b = 6;           // Assign the value 6 to b
c = a + b;       // Assign the sum of a and b to c

When separated by semicolons, multiple statements on one line are allowed:

a = 5; b = 6; document.write("<h1>Hello World</h1>") ;
JavaScript Comments –

JavaScript comments can be used to explain JavaScript code, and to make it more readable. JavaScript comments can also be used to prevent execution, when testing alternative code.

Single Line Comments –

Single line comments start with //.
Any text between // and the end of the line will be ignored by JavaScript (will not be executed).

Example –

In the example below, the first line – “Hello World 1” will not be executed.

//document.write("<h1>Hello World 1</h1>")
document.write("<h1>Hello World 2</h1>")
Multi-line Comments –

Multi-line comments start with /* and end with */.
Any text between /* and */ will be ignored by JavaScript.

Example –

In the example below, both the lines will not be executed.

/*
document.write("<h1>Line 1</h1>");
document.write("Line 2")
*/

Using comments whenever necessary is always a good practice and in real world scenarios it is many times necessary.

Watch it on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *