JS Comments

JavaScript

In this tutorial we will learn about JavaScript comments.

Like most of the programming languages like C++, Java, PHP, C# etc. JavaScript also has comments - Single line comments and Multi line comments.

Single Line Comment

We use the // two forward slash to create single line comment in JavaScript.

In the following example we have created a single line comment.

//this is a single line comment

console.log("Hello World!");	//this will console log "Hello World!" text

Multi Line Comment

We use the /* to start and */ to stop a multi line comment in JavaScript.

In the following example we have created a multi line comment.

//this is a single line comment

/**
 * in the following code we are going to
 * console log "Hello World!" text
 */
console.log("Hello World!");

Why use comments?

We use comments in our code to write some notes or prevent a piece of code from executing.

In the following example we are using both single and multi line comment to create note in our code so that anyone reading the code can understand its purpose.

//create two variables a and b
var a, b;

//setting value of a and b
a = 10;
b = 20;

/**
 * computing the sum of the two variables
 * and using console log to display the result
 */
console.log(a+b);		//this will print 30

In the following example we are using both single and multi line comment to prevent some piece of code from executing.

console.log(1);		//print 1
console.log(2);		//print 2


//following line will note execute as it is commented out using single line comment
//console.log(3);

//following code commented out as well using multi line comment

/*
console.log(4);
console.log(5);
console.log(6);
 */

console.log(7);		//print 7

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT