Comments in JavaScript
Let’s take a quick look at the JavaScript comments, the following are some of important points about the comments.
- Comments in JavaScript are used to explain the code and make the program more readable for developers.
- In programming, comments can also be used to prevent some code lines from being executed. While testing it is more useful.
- There are two types of comments in JavaScript: single-line and multi-line.
- Single-line comment out one line or a part of one line and do not need to be closed.
- Multiline comments can comment out block of code(a few lines) and need to be closed.
Single-line Comments
Single line comments start with //
. Anything that goes after //
until a line break is treated as a JavaScript comment and not executed as code.
// This is a number array var arr1 = [1,2,3,4]; var x = 5; // Variable declaration var y = x + 99; // Different variable
Multi-line Comments
Multi-line comments are more often used for formal documentations. start with /*
and end with */
. Any text inside the /*
and */
will be ignored by JavaScript.
Below is the example for how to use the multi-line comments,
/*This is a number array. We are going to add another element to the arr1 */ var arr1 = [1,2,3,4]; arr1.push(5);
Happy Learning 🙂