JavaScript Comment | javascript comment syntax

JavaScript Comment | javascript comment syntax

JavaScript Comment | javascript comment syntax


Hello, guys welcome to Code With Random Blog, today we learn about JavaScript Comments.

JavaScript Comments are used to make programming easier to read and understand.

Comments are not executed by the browser.

Comments can be used to add notes, suggestions and hints to a JavaScript code.

There are two types of comments in JavaScript,The single-line and the multi-line

Single-Line Comments:-

Ce-line comment, add two forward slash // to the beginning of the line.

this makes all text following it to be a comment.

Here is an example:

      // this is single-line comment 
      function myFunc() {  
         var text = "Welcome to Code with Random";   
         document.getElementByID("demo").innerHTML = text;
    }

Another way is to use the /* */, the comment goes between the asterisks.

This technique is much more flexible. Here is an example:

 /* this is single-line comment */
      function myFunc() {  
         var text = "Welcome to Code with Random";   
         document.getElementByID("demo").innerHTML = text;
    }

Multi-Line Comments:-

To make multi-line comments, use this syntax:

 /* 
    your multi-line comments  
your multi-line comments
your multi-line comments */
function myFunc() {  
         var x = 6;   
         var y = 9;
         var sum = x + y;
         /* let's add the value of y to x
            and then print the sum */
         /*
            you can also comment like this
            sometimes it's more convenient
            and easier to see
         */
         document.getElementByID("demo").innerHTML = sum;
    }

Disabling Code Using Comments

We can disable codes to prevent it from running using comments. This technique is helpful for testing purposes.

In the example below, only the product will be written. Because the writing of the sum to the element will not be executed.
function myFunc() {  
         var x = 6;   
         var y = 9;
         var sum = x + y; // adds x and y
         var product = x * y; // multiplies y to x
 
         document.getElementByID("demo").innerHTML = product;
// document.getElementByID("demo").innerHTML = product; }

Post a Comment

Previous Post Next Post