How to Write Comment in JavaScript

Comment in JavaScript can explain the code, write notes in your code, or disable code sections without removing them. It can prevent code execution while testing alternative code. JavaScript comments make code more readable and understandable.

Why commenting code in JavaScript

The main idea behind commenting on a code is that it allows you to keep track of your code as you do programming, and it gives you the capability to take notes that may be valuable further down the line. Code commenting is one of the core features of many programming languages.

If you are working on a big team, your code is either reviewed by others or analyzed by others, or learned by newbie developers, and writing comments in your code will help ensure that it is readable for everyone.

How to write comments in JavaScript

To write a comment in JavaScript, use the “//”. 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.

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

When we need to comment on only one line of code, we use a single-line comment.

Multi-line comment

When we need to comment on one or more lines, we need to use multi-line comments.

Single line comment in JavaScript

To write a single-line comment in JavaScript, use the “//” before the content. Single-line comments explain the code in a single line. Starting with this symbol to the end of the line will be considered a comment, which the compiler will ignore. Therefore, that code will not execute. 

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.

Multi-line comments in JavaScript

To write multi-line comments in JavaScript, use the /* */ syntax. Multi-line 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. Here it printed only the last console.log() statement, and the compiler will ignore others.

JavaScript Nesting comments

To nest comments in JavaScript, we 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 multi-line 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 that the whole block will be ignored.

That’s it for this tutorial.

Leave a Comment