20 Feb JavaScript Comments
Comments in JavaScript are used to provide extra details about various aspects of a program. They are usually included at the start of a program to indicate its purpose, its author, and the date on which it was written. They also provide clarification about complicated statements in the program.
There are two types of comments used in JavaScript:
- Single-line comments, and
- Multi-line comments
Single-Line Comments
These comments are only on a single line and cannot move beyond that. They are represented using a double slash (//). An example of single-line comments in JavaScript is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<!DOCTYPE html> <html> <body> <h2>Single-line Comments</h2> <p id="test"></p> <script> // Declare a variable and assign value let a = 10; document.getElementById("test").innerHTML = "Value = " + a; </script> </body> </html> |
Output
Multi-Line Comments
These comments can be extended over multiple lines. They are represented using /* i.e slash asterisk (/*……….*/). Multi-line comments cannot be nested. An example of multi-line comments in JavaScript is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<!DOCTYPE html> <html> <body> <h2>Multi-line Comments</h2> <p id="test"></p> <script> /* Declare a variable a and assign a value 10*/ let a = 10; /* Write the value of a to the test id */ document.getElementById("test").innerHTML = "Value = " + a; </script> </body> </html> |
Output
No Comments