Cyber Week

Save big!

All courses under $5 - for a limited time!

Code has been added to clipboard!

Master JavaScript Comments: Single-Line vs. Multi-Line

Reading time 3 min
Published Jan 7, 2016
Updated Oct 1, 2019

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.

Comment vs. Comment Out

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.

Single-line Comments

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.

Multi-line Comments

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";

JavaScript Comments: Summary

  • 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.