in , ,

JavaScript Comments

JavaScript Comments
JavaScript Comments

In this tutorial, you will learn about JavaScript comments, why you should use them, and how to use them with the help of examples.

JavaScript comments can be used to clarify JavaScript code and to make it more clear.

JavaScript comments can likewise be used to prevent execution when testing alternative code.

JavaScript comments are hints that a programmer can add to make their code simpler to read and comprehend. They are totally disregarded by JavaScript engines.


There are two ways to add comments to code:

// – Single Line Comments
/* */ -Multi-line Comments


Single Line Comments

In JavaScript, any line that starts with // is a single line comment. For example,

name = "Salman";

// printing name on the console
console.log("Hello " + name);

Here, // printing name on the console is a comment.

You can likewise use single line comment like this:

name = "Salman";

console.log("Hello " + name);  // printing name on the console

Multi-line Comments

In Javascript, any text between /* and */ is a multi-line comment.


Using Comments for Debugging

comments can likewise be used to disable code to keep it from being executed. For instance,

console.log("some code");
console.log("Error code);
console.log("other code");

If you get a blunder while running the program, rather than eliminating the mistake inclined code, you can use comments to disable it from being executed; this can be a significant debugging tool.

console.log("some code");
// console.log("Error code);
console.log("other code");

Pro Tip: Remember the shortcut for using comments; it very well may be really useful. For most code editors, it’s Ctrl +/for Windows and Cmd +/for Mac.


Make Code Easier to Understand

As a JavaScript developer, you won’t just write code however may likewise need to adjust code written by different developers.

If you write comments on your code, it will be simpler for you to comprehend the code later on. Additionally, it will be simpler for your fellow developers to comprehend the code.

As a general rule of thumb, use comments to clarify why you accomplished some different option from how you accomplished something, and you are good.

Note: Comments shouldn’t be the substitute for an approach to clarify poorly written code in English. You ought to consistently write all around organized and self-explanatory code. Furthermore, at that point use comments.


Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.

salman khan

Written by worldofitech

Leave a Reply

Java Command-Line Arguments

Java Command-Line Arguments

How to Send Disappearing Messages with WhatsApp

Step by step instructions to Send Disappearing Messages with WhatsApp