To write a comment in JavaScript, you can use the “//” syntax. Any text between “//” and the end of the line will be ignored by JavaScript, which means it won’t be executed. Every character following the “//” statement will be ignored until the end of the line.
Comments help explain your code or leave notes for yourself and other developers who might work on the code in the future.
Example
let data = 19; // Declare data, give it the value of 19
let info = data + 21; // Declare info, give it the value of data + 21
JavaScript compiler executes the code except after “//”.
Types of comments in JavaScript
Single line comment
To write a single-line comment, start with two forward slashes (//) followed by your comment. The JavaScript engine will ignore everything after the slashes until the end of the line.
Syntax
// This is a single line comment.
Example
// Declare a constant with string value
const name = "Iron man";
// Calculator function
function calculator() {
// Some code
}
console.log(name);
// console.log('this will not print')
Output
Iron man
In the above example, we explain the code by single-line comment. We commented on the last console log, so it can not be executed.
Multiline comments
To write multiline comments in JavaScript, you can use the “/* */” syntax. Multiline comments are used to prevent the execution of many lines of code. They also used a detailed explanation of the working of codes. However, all the lines inside the /* */ syntax won’t be executed.
Syntax
/* This is
Multiline
Comment
*/
Example
/*
We can write anything here.
console.log("this is multiline comment");
function calculator(){
// some code
}
*/
console.log('Avengers the multiline');
Output
Avengers the multiline
In the above example, we commented on some multiline code that would not be executed. As a result, it printed only the last console.log() statement, and the compiler will ignore others.
Nesting comments
To nest comments in JavaScript, you can write a single-line comment inside a multiline comment but can not write a multiline comment inside a single line.
Example 1
// /*
This is comment
*/
Output
SyntaxError: Unexpected identifier
This gives an error because multiline comments can not be used in a single line by changing the line. Of course, we can write the multiline comment in a single line, but the line can not be changed; otherwise, it will be an error.
Example 2
/*
// This is comment
*/
In the example, we can see a single-line comment inside the multiline comment.
This is perfectly valid syntax. The compiler considers a single-line comment as part of the multiline comment, so the whole block will be ignored.
That’s it.

Niva Shah is a Software Engineer with over eight years of experience. She has developed a strong foundation in computer science principles and a passion for problem-solving.