Comments in JavaScript

 
Mostly Comments are used to explain the JavaScript code so that end user can understand your code and easy to readable.
There are two types of comment we can use in java script

1. Single line comments.
2. Multi Line comment.

Single Line Comment

Single line comments start with // and use to comment one line at a time
Example

<script type="text/javascript">
    // This is comment line
    document.write("<h2>Header Line</h2>");
    // Following are two paragraphs:
    document.write("<p> paragraph1.</p>");
    document.write("<p> paragraph2.</p>");
</script>

Multi Line Comment

Multi Line comment start with /* and end with */ and use to comment a block of code or more than one line at a time.
Example

<script type="text/javascript">
     /*
     Comment more than one line
     At a single time
    */
    document.write("<h2>Header Line</h2>");
    document.write("<p> paragraph1.</p>");
    document.write("<p> paragraph2.</p>");
</script>

We can also comment code line to prevent execution as follows

Single line

<script type="text/javascript">
    // document.write("<h2>Header Line</h2>");
    document.write("<p> paragraph1.</p>");
    document.write("<p> paragraph2.</p>");
</script>

Multi Line

<script type="text/javascript">
    /* document.write("<h2>Header Line</h2>");
    document.write("<p> paragraph1.</p>");
    document.write("<p> paragraph2.</p>"); */
</script>

We can also use comments after code line

<script type="text/javascript">
    document.write("<h2>Header Line</h2>"); // this is Header line
    document.write("<p> paragraph1.</p>");
    document.write("<p> paragraph2.</p>");
</script>

 

Author: Ankur

Have worked primarily in the domain of Calling, CRM and direct advertisers services. My technological forte is Microsoft Technologies especially Dot Net (Visual Studio 2003, 2005, 2008, 2010 and 2012) and Microsoft SQL Server 2000,2005 and 2008 R2. My Area of Expertise is in C#. Net, VB.Net, MS-SQL Server, ASP. Net, Silverlight, HTML, XML, Crystal Report, Active Reports, Infragistics, Component Art, ComponeOne, Lead Tools etc.