JavaScript | Comments

Comments are used to prevent the execution of statements. Comments are ignored while the compiler executes the code. Comments are user friendly as user can get explanations of code using comments.

Syntax:

// For single line comment
/* For block of lines comment
...
...
*/

Return Value: During execution of code, comments are ignored.

Example 1: This example illustrates the single line comment using //.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        JavaScript Comments
    </title>
      
    <script>
          
        // Function to add two numbers
        function add() {
              
            // Declare three variables
            var x, y, z;
              
            // Input a number and store it into variable x
            x = Number( document.getElementById("num1").value );
              
            // Input a number and store it into variable x
            y = Number( document.getElementById("num2").value );
              
            // Sum of two numbers
            z= x +y;
              
            // Return the sum
            document.getElementById("sum").value = z;
        }
    </script>
</head>
  
<body>
    Enter the First number: <input id="num1"><br><br>
    Enter the Second number: <input id="num2"><br><br>
      
    <button onclick="add()">
        Sum
    </button>
      
    <input id="sum">
</body>
  
</html>                    

chevron_right


Output:
Before clicking on the button:

After clicking on the button:



Example 2: This example illustrates the multi-line comment using /* … */

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        JavaScript Comments
    </title>
      
    <script>
      
        /* Script to get two input from user
        and add them */
        function add() {
              
            /* Declare three variable */
            var x, y, z;
              
            /* Input the two nos. num1, num2
            Input num1 and store it in x
            Input num2 and store it in y
            The sum is stored in a variable z*/
            x = Number(document.getElementById("num1").value); 
            y = Number(document.getElementById("num2").value);
            z = x + y;
              
            document.getElementById("sum").value = z;
        }
    </script>
</head>
  
<body>
    Enter the First number : <input id="num1"><br><br>
    Enter the Second number: <input id="num2"><br><br>
      
    <button onclick="add()">
        Sum
    </button>
      
    <input id="sum">
</body>
  
</html>                    

chevron_right


Output:
Before clicking on the button:

After clicking on the button:

Example 3: This example illustrates that commented code will never execute.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        JavaScript Comments
    </title>
      
    <script>
          
        function add() {
              
            var x, y, z;
              
            x = Number( document.getElementById("num1").value );
              
            y = Number( document.getElementById("num2").value );
              
            z= x +y;
              
            // Comment the code section
            //document.getElementById("sum").value = z;
        }
    </script>
</head>
  
<body>
    Enter the First number: <input id="num1"><br><br>
    Enter the Second number: <input id="num2"><br><br>
      
    <button onclick="add()">
        Sum
    </button>
      
    <input id="sum">
</body>
  
</html>                    

chevron_right


Output:
Before clicking on the button:

After clicking on the button:

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.