Looking for JavaScript comment?

Being a programmer it is crucial that you keep track of code blocks and their various sections. Moreover, it’s a good practice to document the work.

Not only it helps you to understand. But also give a meaningful explanation to other developers, who might work on the same project.

JavaScript or any other programming language comments are used everywhere.

Types of Comment in JavaScript

There are two types of comments used in JavaScript-

  • Single line comment in JavaScript
  • And Multi-line comment

Single line Comment in JavaScript

A single line comment in JavaScript is denoted by a double slash //

If you add // to the beginning, the whole line is treated as a comment. Please note that comments are not executed by JavaScript.

Thus, anything after // is ignored ( single line only).

// Display a message:
let message = "Hi this is JavaScript";    //Assign a message
console.log(message);                     //Display it on console

By convention, a line starting with  // acts as a header. And it is often added at the end of a line to explain the line of code.

This is how we comment out in JavaScript.

Multi-line Comments in JavaScript

JavaScript also supports multi-line comments.

To add multi-line comments in javascript you need to use /* at the start and */ at the end. Anything in between /* and */ is treated as a comment and will be ignored by JS engines.

/* Hi this is a JavaScript program to
help you understand multi-line comments.
See the code below */

let x=1;
let y=2;

let sum= x+y;

/* let message = "Welcome to Letstacle.com";    //Assign a message
console.log(message);                     //Display it on console

Multi-line comments are used mainly to comment out a block of code at one go. It is also used to document a paragraph by including a more detailed explanation.

Note: We have used multi-line comments in the above example to exclude the execution of a code block. See lines 12 to 14 as a reference.

Other use of JavaScript Comment

Programmers also use comments to quickly debug and do temporary bug fixes. Allowing not to delete the existing line/block of code but to simply comment it out.

Certainly, comments play an important role.

That’s all about JavaScript comments. Also, You may be interested to learn how to link JavaScript to HTML.

Like this article? Follow us on Facebook and LinkedIn. You can also subscribe to our weekly Feed