How do you Comment Code in JavaScript?

In JavaScript writing something inside the code which we don’t want to execute by the browser is called comments, these comments can be instructions, logic steps, pseudo-code or sometimes we comment on the code for testing and debugging.

In JavaScript starting something with double forward slashes is a comment and this will be ignored by the browser, it means it will not execute. And this can be anything in ordinary language or code. And there are two types of comments single-line comments and multi-line comments.

JavaScript comments can be used to prevent code execution when testing or debugging. There are two types of comments single-line comments and multiline comments

Prevent Code Execution

During the testing and debugging of the feature sometimes we need to prevent specific code sections from execution. JavaScript comments help to prevent code section execution.

// Comment the code section
//document.getElementById("sum").value = z;

In this example, both lines are commented and the browser will ignore these lines,

Single Line Comment

The single line comments start with a double forward slash (//) after this code all of the words will be commented and the browser will ignore the line.

Multiline Comment

Multiline code comment as know block comments in the javascript is very easy just start with a forward slash and static (/*) and end with (*/) it will comment the code block and the browser will ignore these lines to execute.

/*
The below code will set the 
page title and subtitle 
*/
document.getElementById("pageTitle").innerHTML = "Page Title";
document.getElementById("sub-title").innerHTML = "Sub Title";

In the above example we commented on the hint code which is starting with the forward-slash (/*) plus steric and then the content of the hint closes with the asteric and forward slash (*/) so all of the text between this will be ignored by the browsers.

Conclusions

Working with JavaScript, we can use the JavaScript comments for hints, explanations, and steps of logic, or prevent the specific code section to execute with single-line comments or multiline- comments all of the commented code will be ignored by the browsers.