JavaScript Comments

 

JavaScript comments can be used for comment any JavaScript code or using JS comment you can describe your code which make easier for other developer to know about code. 

The line or code written in comment cannot be execute. 


There are two types of comments in JavaScript :- 

  1. Single line comments 
  2. Multi-line comments

Now lets know about both type of comments one by one .


1) Single line comments

In single line comments we can comment a single line using // . 

 

For Example 

<!DOCTYPE html>  

<html lang="en">  

  <head>  

    <meta charset="UTF-8" />  

    <meta http-equiv="X-UA-Compatible" content="IE=edge" />  

    <meta name="viewport" content="width=device-width, initial-scale=1.0" />  

    <title>JavaScript Tutorial</title>  

  </head>  

  <body>  

    <h4>JavaScript comment</h4>  

       <p id="demo_js"></p> 

       <script>  

       var a = 8;      // Declare x, give it the value of 8 

       var b = a + 3;  //Declare y, give it the value of x+3 

      // Write b to demo_js 

document.getElementById("demo_js").innerHTML = b;  

    </script>  

  </body>  

</html> 

 

Output 

image 

 

2) Multi-line comments 

In multi-line comments we can comment multiple line using /* comment here */ . 

 

For Example 

<!DOCTYPE html>  

<html lang="en">  

  <head>  

    <meta charset="UTF-8" />  

    <meta http-equiv="X-UA-Compatible" content="IE=edge" />  

    <meta name="viewport" content="width=device-width, initial-scale=1.0" />  

    <title>JavaScript Tutorial</title>  

  </head>  

  <body>  

    <h4>JavaScript comment</h4>  

       <p id="demo_js"></p> 

       <script>  

      /* var x=5; 

      var y=23; */

       var a = 8;      

       var b = a + 3;   

      // Write b to demo_js

document.getElementById("demo_js").innerHTML = b;  

    </script>  

  </body>  

</html> 

 

Output 

image