Javascript Comments

Comments allow you to add notes or hints to the JavaScript code. When executing the code, the JavaScript engine ignores the comments.

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

JavaScript comments are mostly used to prevent execution.

  • There are mainly two types of JavaScript comments which are as follows.
  1. Single Line Comments
  2. Multi-line Comments

We will get the details of both these types of comments in detail.

Single Line Comments

This single-line comment begins with the Tavo forward-slash characters (//).

Single-line comment // Makes all text on the same line into a comment

  • You can understand single line comments by following example.
 
 <script>
  var x, y;  // Declare Variables
  x = 5;     
  y = x + 2; 
  document.write(y); // Variables give it the value of y
 </script>             

Multi-line Comments

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

Any text between the letters / * and * / is considered a comment. This can spread to multiple lines.

Text between /* and * / will be ignored by JavaScript.

  • You can understand Multi-line comments by following example.
 
 <script>
  / *
   This is a multi-line comment in JavaScript
   It's similar to the comments in programming
   * /
 </script>             

  • In the next lecture we will discuss JavaScript variables Will be discussed in detail.