tutorialstonight logo

JAVASCRIPT COMMENT


Javascript comment is used to write comments within the code to make the code more readable and for future references.

The comments written within the program is ignored and is not executed. So it can be used to prevent some code from execution when you comment it out.

Importance Of Comments In Programming

Importance of comments in programming is as follows:

  1. Comments in code give extra information about the code like some variable, function use, etc.
  2. Commenting code makes code more readable and easy to understand for the developers.
  3. Tricks used within the program can be explained within the 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 it in future.

Note: Writing comments in code is highly recommended.


How to comment in javascript?

Single Line Comment In Javascript

To create a single line comment in javascript place 2 slashes // in front of the code or the text you which to comment out.

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

▶ Try It

Using comment to prevent execution

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

▶ Try It

Javascript Comment Multiple Lines

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

multi-line comment starts with /* and ends with */. The example below uses a multi-line comment for better understanding.

▶ Try It

Using comment to prevent the execution

Commenting out multiple lines of code using multi-line comments in javascript.

▶ Try It