Home >>Javascript Tutorial >Javascript Comments
JavaScript comments are used to elaborate JavaScript code that make it more readable. When testing alternative code, in order to prevent execution JavaScript comments are used.
There are 2 types of Comments we can define in JavaScript
The Single line comments generally start with //.
A single-line comment is used here before each code line in this example:
Example 1 :
//this is single line comments alert("Welcome Phptpoint");
Example 2 :
// Declare a, give it the value of 8 var a = 8; // Declare b, give it the value of x + 6 var b = a + 6;
The Multi-line comments generally start with /* and end with */.
Any text written between /* and */ will be ignored by JavaScript.
A multi-line comment has been used in this example to explain the code:
Example
/* The code below will change the heading with id = "myH" and the paragraph with id = "myP" in my web page: */ document.getElementById("myH").innerHTML = "My World"; document.getElementById("myP").innerHTML = "My first World.";
Let's understand comments with another example:
A line will be treated as a comment if it begins with two forward slashes:
// Note to self: comments are good.
If you use this notation, you must put the slashes at the start of each comment line. This won’t work, for instance:
// Note to self: comments are good.
Instead, you’d need to write
// Note to self: // comments are good.
In order to comment out multiple lines like that, a forward slash can be placed and an Asterisk should be placed at the start of the comment block and an asterisk and forward slash at the end:
/* Note to self: comments are good */