JS Comment : Hiding Code
In order to make JavaScript more readable we should use as many as comments in Script. Comment is non executable Statement in the JavaScript.
Also See : Statements in JavaScript
Types of Comment in JavaScript
We can have two types of comment in JavaScript -
- Single Line Comment
- Multiple Line Comment
Single Line Comment in JavaScript
- Single Line comment starts with “//” symbol.
- Remaining line after “//” symbol is ignored by browser.
- End of Line is considered as End of the comment.
Example of Single Line Comment :
<script type="text/javascript"> // This is First Single Line Comment document.write("<p>This is a paragraph 1</p>"); // Write two paragraphs document.write("<p>This is a paragraph 2</p>"); document.write("<p>This is a paragraph 3</p>"); </script>
Multiple Line Comment in JavaScript
- Multi Line comment starts with “/*” symbol.
- Multi Line comment ends with “*/” symbol.
- Content written inside the multi-line comment is ignored by the browser.
Example of Multiple Line Comment :
<script type="text/javascript"> /* This is Multiline Comment */ document.write("<p>This is a paragraph 1</p>"); document.write("<p>This is a paragraph 2</p>"); document.write("<p>This is a paragraph 3</p>"); </script>
How to use Comment more efficiently in JavaScript
- Comment used for Documentation.
- Comment used to Hide Non-usable Code.
- Comment used to Hide Single Statement.
- Comment written after Statement
1. Comment used for Documentation
<script type="text/javascript"> /* This code is used to create function which will call Google API for gmail. using this API we can view Gmail in PHP */ </script>
Above comment is used for documentation. We use This type of Documentation to summarize JS Code.
2. Comment used to Hide Non-usable line.
<script type="text/javascript"> // document.write("<h1>Heading</h1>"); document.write("<p>Google</p>"); document.write("<p>Yahoo</p>"); </script>
We have used comment to prevent execution of single line.
// document.write("<h1>Heading</h1>");
3. Comment used to Hide Non-usable Multiline Code.
/* document.write("<h1>Heading 1</h1>"); document.write("<h1>Heading 2</h1>"); document.write("<h1>Heading 3</h1>"); document.write("<h1>Heading 4</h1>"); */
4. Comment written after Statement
<script type="text/javascript"> document.write("Hello "); // Write "Hello" document.write("World"); // Write "World" </script>