ADVERTISEMENT

How to Add Comment In JavaScript? JavaScript comment syntax?

Telegram Group Join Now

ADVERTISEMENT

How to Add Comment In JavaScript? JavaScript comment syntax?

 

ADVERTISEMENT

How to Add Comment In JavaScript ? JavaScript comment syntax?
How to Add Comment In JavaScript ? JavaScript comment syntax?

ADVERTISEMENT

 

 

ADVERTISEMENT

Hello, guys welcome to Codewithrandom Blog, Today we learn about JavaScript Comments. JavaScript Comments are used to make programming easier to read and understand.

ADVERTISEMENT

Gym Website Using HTML and CSS Source Code

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 Comment

ADVERTISEMENT

Single-Line Comments in JavaScript:-

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.

ADVERTISEMENT

Age Calculator Using HTML,CSS and JavaScript

ADVERTISEMENT

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 In JavaScript:-

To make multi-line comments, use this syntax:

/* 
   your multi-line comments  
   your multi-line comments   
   your multi-line comments
*/

Disabling Code Using Comments in JavaScript

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;
    }
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.
document.getElementByID("demo").innerHTML = product;
// document.getElementByID("demo").innerHTML = product;
}

Thanks for reading! Do contact me via the methods below if you have to say anything:

written by @codingporium

ADVERTISEMENT

ADVERTISEMENT

Telegram Group Join Now

Share on:

Leave a Comment

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT