OneBite.Dev - Coding blog in a bite size

write comments in javascript

Code snippet for how to how to write comments in javascript with sample and detail explanation

How to Write Comments in JavaScript

JavaScript is a prime language for web development, making the world of internet more interactive and dynamic. To improve the readability and maintenance of the code, adding comments is a crucial practice. This article will guide you on how to write comments in JavaScript.

Single-Line Comments

In JavaScript, you can write a single-line comment starting with two forward slash characters //.

// This is a single line comment in JavaScript

Code Explanation for Single-Line Comments

The JavaScript interpreter ignores the part of code that comes after // on the same line. This is a common practice when we need to write some information or explanation regarding the corresponding code.

Multi-Line Comments

In addition to single line comments, JavaScript also allows multi-line comments that start with /* and end with */.

/* 
This is a 
multi-line comment 
in JavaScript 
*/

Code Explanation for Multi-Line Comments

When a comment expands beyond a single line, or you need to prevent a block of code from being executed without deleting it, multi-line comments are used. Anything between /* and */ is ignored by the JavaScript interpreter.

Conclusion

A well-documented code is as crucial as the functionality of the code itself. Commenting code in JavaScript allows the programmer to leave valuable notes for any future reference. Whether you are working in a team or on a personal project, incorporating comments is an industry-standard best practice that every coder should implement.

javascript