JavaScript Comment

JavaScript comment provide meaningful ways to communicate messages. JavaScript comments have the benefit of making code easier to understand. When writing code, you could encounter some difficult, illogical logic; this is the ideal time to add comments to the code that will clarify what is happening. Your code will be clear enough for anyone to comprehend if they examine it later.

In JavaScript, there are two types of comments.

  • Single-line Comment
  • Multi-line Comment

Single line Comment Javascript

We use // for Single-line Comment. Anything after // will be ignored by JavaScript on that line.

Lets take a look at an example of a single-line comment.

// This is an example of single line comment
document.write("Hello World"); 

Multiple line Comment Javascript

A block comment is sometimes called a multi-line comment. Anything between /* and */ will be ignored.

It starts with /* and ends with */

/* This is an example of Multi line comment
Below code will print "Hello World" */

document.write("Hello World"); 

Comments practices

Code should not be repeated in comments.

Comments must solve confusion rather than create it.

When fixing bugs, include comments.

Create meaningful comments for methods and functions.