There are two ways to add comments to code:
// – Single Line Comments
/* */ -Multi-line Comments
Single Line Comments
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
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
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.