There are comments and information about the comment line used to explain the JavaScript codes or disable the execution of the codes.

JavaScript’s comments are used to explain written JavaScript codes and increase code readability.

Comment line

Single-line comments begin with two slashes //.

The two slashes after // will be interpreted as a comment line and will not be executed by JavaScript.

In the example, before each JavaScript code, information about what the code will do is given as a comment.

// variable x is defined
var x;
// the value of 5 is assigned to the variable x
x = 5;

In the example below, information about what the code will do after the code is given as a comment.

var x; // variable x is defined
x = 5; // the value of 5 is assigned to the variable x

Multi-line comments

Multiline comments begin with /* and end with */.

JavaScript code and text between /* and */ will not be executed.

/*
variable x is defined
variable x is assigned a value of 5
*/
var x;
x = 5;

Disable your codes

When experimenting with JavaScript codes, we can use comments to disable code that we do not want to run.

Disable single line code;

var x;
x = 5;
// alert(x * 10);

Disable multiple lines of code;

/*
var x;
x = 5;
alert(x * 10);
*/