JS Comment : Hiding Part in JavaScript
In order to make JavaScript more readable we should use as many as comments in Script. Comment is non executable Statement in the 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
<script type="text/javascript"> // This is First Single Line Comment document.write("<h1>Welcome to JavaScript World</h1>"); // Write two paragraphs document.write("<p>This is a paragraph 1</p>"); document.write("<p>This is a paragraph 2</p>"); </script>
- Single Line comment starts with “//” symbol.
- Remaining line after “//” symbol is ignored by browser.
- End of Line is considered as End of the comment.
Multiple Line Comment in JavaScript
<script type="text/javascript"> /* This is Multiline Comment */ document.write("<h1>Welcome to JavaScript World</h1>"); document.write("<p>This is a paragraph 1</p>"); document.write("<p>This is a paragraph 2</p>"); </script>
- Multi Line comment starts with “/*” symbol.
- Multi Line comment ends with “*/” symbol.
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>