🔥 $100K Hit! Where Will Bitcoin Go Next? Find Out Live!

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.

DataCamp
Pros
  • Easy to use with a learn-by-doing approach
  • Offers quality content
  • Gamified in-browser coding experience
  • The price matches the quality
  • Suitable for learners ranging from beginner to advanced
Main Features
  • Free certificates of completion
  • Focused on data science skills
  • Flexible learning timetable
Udacity
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
edX
Pros
  • A wide range of learning programs
  • University-level courses
  • Easy to navigate
  • Verified certificates
  • Free learning track available
Main Features
  • University-level courses
  • Suitable for enterprises
  • Verified certificates of completion

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.