How many types of comments are in javascript?

  1. JavaScript comments
  2. Advantage of javaScript comments
  3. Single-line and Multi-line comments

The JavaScript comments are meaningful way to deliver message. It is used to add information about the code, warnings or suggestions so that end user can easily interpret the code.

The JavaScript comment is ignored by the JavaScript engine i.e. embedded in the browser.

Advantages of JavaScript comments

There are mainly two advantages of JavaScript comments.

  1. To make code easy to understand It can be used to elaborate the code so that end user can easily understand the code.
  2. To avoid the unnecessary code It can also be used to avoid the code being executed. Sometimes, we add the code to perform some action. But after sometime, there may be need to disable the code. In such case, it is better to use comments.

Types of JavaScript Comments

There are two types of comments in JavaScript.

  1. Single-line Comment
  2. Multi-line Comment

JavaScript Single line Comment

It is represented by double forward slashes (//). It can be used before and after the statement.

Let’s see the example of single-line comment i.e. added before the statement.

Test it Now

Let’s see the example of single-line comment i.e. added after the statement.

Test it Now

JavaScript Multi line Comment

It can be used to add single as well as multi line comments. So, it is more convenient.

It is represented by forward slash with asterisk then asterisk with forward slash. For example:

It can be used before, after and middle of the statement.

Test it Now


JavaScript comments can be used to explain JavaScript code, and to make it more readable.

JavaScript comments can also be used to prevent execution, when testing alternative code.


Single Line Comments

Single line comments start with //.

Any text between // and the end of the line will be ignored by JavaScript (will not be executed).

This example uses a single-line comment before each code line:

Example

// Change heading:
document.getElementById("myH").innerHTML = "My First Page";

// Change paragraph:
document.getElementById("myP").innerHTML = "My first paragraph.";

Try it Yourself »

This example uses a single line comment at the end of each line to explain the code:

Example

let x = 5;      // Declare x, give it the value of 5
let y = x + 2;  // Declare y, give it the value of x + 2

Try it Yourself »


Multi-line Comments

Multi-line comments start with /* and end with */.

Any text between /* and */ will be ignored by JavaScript.

This example uses a multi-line comment (a comment block) to explain the code:

Example

/*
The code below will change
the heading with id = "myH"
and the paragraph with id = "myP"
in my web page:
*/
document.getElementById("myH").innerHTML = "My First Page";
document.getElementById("myP").innerHTML = "My first paragraph.";

Try it Yourself »

It is most common to use single line comments.
Block comments are often used for formal documentation.



Using Comments to Prevent Execution

Using comments to prevent execution of code is suitable for code testing.

Adding // in front of a code line changes the code lines from an executable line to a comment.

This example uses // to prevent execution of one of the code lines:

Example

//document.getElementById("myH").innerHTML = "My First Page";
document.getElementById("myP").innerHTML = "My first paragraph.";

Try it Yourself »

This example uses a comment block to prevent execution of multiple lines:

Example

/*
document.getElementById("myH").innerHTML = "My First Page";
document.getElementById("myP").innerHTML = "My first paragraph.";
*/

Try it Yourself »



Programming languages offer an option of leaving notes for yourself or others. The JavaScript comment feature simplifies the production of more readable code. They are easy to write and recognize: a JavaScript comment block (also known as the multi-line comment) begins with */ , while a single line comment starts with //.

In this quick tutorial, you'll learn how to write JavaScript comments and be able to leave notes in your JavaScript code. JavaScript is not the only programming language to have the comment function in their syntax: HTML offers it as well.

JavaScript Comments: Main Tips

  • Comments in JavaScript are used to explain the code and make the program more readable for developers.
  • In programming, comments can also be used to prevent some code lines from being executed. This is useful when testing.
  • There are single-line comments (which comment out one line or a part of one line) and multi-line comments (which comment out a block of code).
  • Multi-line comments are more often used for formal documentations.

The JavaScript commenting out means that you take a part of the code and surround it with JavaScript comment symbols. Comments are not executed by the browser, which means that they won't be displayed.

Check out the example of commenting out a single-line below:

Example

//document.getElementById("test_el").innerHTML = "Some text";  
document.getElementById("test_el2").innerHTML = "Some more text";

This example illustrates the commenting out a block of code:

Example

/* document.getElementById("test_el").innerHTML = "some text";  
   document.getElementById("test_el2").innerHTML = "some more text";  */

Therefore, the JavaScript comment lets you comment a line, a part of it, or a block of code. This feature can be used for debugging your code to find the location of a bug quickly. Comment out a part of the code so you don't have to delete a part of it to see whether a bug is in that line.

How many types of comments are in javascript?

Pros

  • Simplistic design (no unnecessary information)
  • High-quality courses (even the free ones)
  • Variety of features

Main Features

  • Nanodegree programs
  • Suitable for enterprises
  • Paid certificates of completion

How many types of comments are in javascript?

Pros

  • Easy to navigate
  • No technical issues
  • Seems to care about its users

Main Features

  • Huge variety of courses
  • 30-day refund policy
  • Free certificates of completion

How many types of comments are in javascript?

Pros

  • Great user experience
  • Offers quality content
  • Very transparent with their pricing

Main Features

  • Free certificates of completion
  • Focused on data science skills
  • Flexible learning timetable

Single-line comments are used to comment a part of a line or a full line of code in JavaScript. You can use it for either explaining the code or debugging (commenting out to prevent the execution by the browser).

The example below uses a single-line comment to explain a line of code below the comment:

Example

// This is a code in JavaScript:
document.getElementById("stuff2").innerHTML = "This is also a sentence that gives no information";

In the example below, a comment is written on the same line as the code itself:

Example

var x = 5;       // Variable declaration
var y = x + 99// Different variable

For a single-line comment, you have to write two forward slashes: //. Anything that goes after // up until a line break is treated as a JavaScript comment and not executed as code.

JavaScript multiline comment has the same purpose as a single-line comment. JavaScript comment block starts with two symbols: /*. To end the comment, use these symbols */ again. Everything between these symbols will be treated as a comment in JavaScript.

You can use JavaScript multiline comment for leaving a long comment or commenting out larger parts of code while debugging:

Example

/* JavaScript
  code  */

document.getElementById("test_el").innerHTML = "Some text"; 
document.getElementById("test_el2").innerHTML = "More text";

  • There are two types of comments in JavaScript: single-line and multi-line.
  • Comments can be used to explain or hide the code so it is not executed.
  • Single-line JavaScript comments are used for one line of comment only and do not need to be closed.
  • Multiline comments in JavaScript can comment out bigger parts (a few lines) of code and need to be closed.

How many types of comment are there?

There are mainly two types of comments as follows: Line comment. Block comment.

What is comments in JS?

JavaScript comments can be used to explain JavaScript code, and to make it more readable. JavaScript comments can also be used to prevent execution, when testing alternative code.

What is the right kind of JavaScript comment?

In JavaScript, single-line comments begin with // . It will ignore all the things immediately after // syntax until the end of that line. This is also known as inline commenting when we use // syntax alongside codes lines.

Which symbol is used for comments in JavaScript?

Example 1: This example illustrates the single line comment using //.