Single line comment –
If we write any line starting with two forward slashes (//) then that particular line would become a comment.
Note : Sometimes we prevent execution of a certain part of our code by making it a comment while testing the code.
Example of a single line comment
The line we have written after // is to explain the use of document.write().
var num = 4; //The above line would print 4 document.write(num);
Preventing execution of code by making it a comment.
var num = 4; //This line would print 4 //document.write(num)
The above example would print nothing, as we have commented the line document.write(num).
Multi line comments
A multi line comment can be written by starting anything with /* and ending it with */.
Example of a multi line comment:
/*The code written below will create a variable named x, assign it a value of 7, and print it.*/ var x = 4; document.write(x);
Preventing a bunch of lines of code by multi line comments:
var x = 4; document.write(x); /*document.write("Hello") document.write("Welcome")*/
In above example, the value of the variable would be displayed, but the messages “Hello” and “Welcome” would
not, because we have commented them.
Note : Comments can be written anywhere, but it is a good practice to write a comment before a particular code.
- At times you might need to temporarily avoid some part of our code, for that you can simply make that part a comment.