Tutorials Tonight
Follow Us

JAVASCRIPT COMMENT


In this tutorial, you will learn to write comments in javascript. You can write single-line comments and multi-line comments. We will also look at why it is important to write comments in javascript.

  1. Single-Line Comments
  2. Multi-Line Comments
  3. Importance of writing comments
  4. FAQ

Javascript comment is used to write comments within the programs to explain the purpose of code snippets, helps others to understand the code, and helps to maintain the code.

The comments written within the program are ignored and are not executed by compilers or interpreters.

You can use this property of comment to prevent a code snippet from being executed by commenting it out.

There are two types of comments in javascript:

  • Single line comment
  • Multi-line comment

Single Line Comment

Single line comments create for only one line of code.

Single line comments are written using the // symbol. Anything written after the // symbol in that line becomes part of the comment.

When you place 2 slashes // then the code or text of the current line is ignored until the next line.

The following code is a single-line comment.

Example

// This is a single line comment
console.log("Welcome to TutorialsTonight");

// What is your purpose?
console.log("Learning Javascript!");

// code before the comment is executable
var num = 10; // num represents number
console.log(num);
▶ Try It

Using comment to prevent execution

You can use single-line comments to prevent code from being executed by commenting out the code.

Commenting out code using a single-line comment. Uncomment the code below and run to understand.

Example

for (let i = 0; i <= 5; i++) {
  console.log("Number = " + i);
  // console.log("Square of number = " + i*i + " ");
}
▶ Try It

Multi-line Comment

When you need to comment out a large block of code then commenting code using single-line comment is quite burdensome. To comment out multiple lines of code use multi-line comments.

Multi-line comments are used to write comments for multiple lines of code.

Multi-line comments are written using the /* and */ symbols.

Anything that is written between the /* and */ symbols in single or multiple lines is as a comment.

The following code is a multi-line comment.

Example

/* This is 
a multi-line comment

alert("This is commented");

only code outside this
multi-line comment will run
*/
console.log("Learning multi-line comment");
▶ Try It

Using multi-line comment to prevent the execution

You can use multi-line comments to prevent a large block of code from being executed by commenting out the code.

Example

/* alert("this is commented")
console.log("this is also commented")
for(let i = 0;i < 10;i++)
  console.log("all of these are commented");
*/
console.log("This will execute!");
▶ Try It

Importance Of Comments In Programming

The importance of comments in programming is as follows:

  1. Comments in code give extra information about the code like the use of some variable, function, etc.
  2. Commenting code makes code more readable and easy to understand for the developers and maintainers.
  3. Tricks used within the code can be explained using comments.
  4. It can stop a block of code from execution if you don't want to execute for some case.
  5. Comments help to understand code when you refer to it in the future.

Note: Writing comments in code is highly recommended.


Frequently asked questions

  1. How many types of comments?

    There are two types of comments in javascript:

    • Single line comment
    • Multi-line comment
  2. What is the difference between single line comment and multi-line comment?

    Single line comment is used to comment a single line of code. Multi-line comment is used to comment a large block of code.

  3. How do you comment a script tag?

    You can comment a script tag using the <!-- --> symbol and your script tag in between the 2 dashes (). Example <!-- <script> -->

  4. What is the shortcut to comment out in JavaScript?

    You can comment out a line of code using the Ctrl + / shortcut for single line comment. For multi-line comment select the lines of code and press Ctrl + /.

  5. What is the correct syntax for adding comments in JavaScript?

    You can add comments in JavaScript using the following syntax:

    • Single line comment: // comment
    • Multi-line comment: /* comment */


Follow Us

Copyright © Tutorials Tonight. All Rights Are Reserved.