Javascript Comments
In any programming language, a comment is a statement or a piece of code that is ignored by the browser/compiler. Comments can be used to explain the functionality of a particular code, or to increase its readability.
In Javascript, comments can be added in the following ways:
//
- A Single line comment/* */
- A Multi line comment
Including Single Line Comments
Single line comments can be added using //
.
Let's have a look at an example:
//This is a single line comment
//A variable 'x' is declared below
var x;
Live Demo!
Including Multi Line Comments
Multi line comments can be added using /* */
. Let's check an example:
/*This is
a multi line comment
*/
/*
A variable declaration
*/
var y;
Live Demo!
Preventing Code Execution
Javascript comments can also be used to prevent the code from execution, and it can be for testing purposes. Adding a //
in before a line changes it from executable code to a comment.
Example:
//Commenting both the lines
//var x = 34 + 5;
//document.write(x);
Live Demo!