JavaScript Comment and Syntax

    javascript comment and syntax

    JavaScript Comment

    The JavaScript comment syntax is used to convey messages and by using comment programmers can add the description of the code. 

    It is very useful for both users and programmers. Users can easily understand the code.

    JavaScript engine ignores the comments in the JavaScript program .

    Benefit using JavaScript comments

    Following are the benefits of JavaScript comments.

    1: It is used for comfort of the user ,with the help of comment users can easily interpret the code.

    2: Sometimes we do not want some code to be executed ,in this case instead of deleting that code we can apply a comment on that code .

    For example: we wrote some code for one time execution after execution we do not want to run the code so instead of deleting that code, it will be good to use comments .

    Different Types Comments in JavaScript

    We can use two types of comments. 

    1. Single-line Comment
    2. Multi-line  Comment

    Single line JavaScript Comment

    In a single line Comment we use (//) Double forward slashes.

    Programmers can write it before or after the statement as per their requirement.

    Following are the examples of single-line comment.

    1: Added the comment before the JavaScript statement.

    1. <script>
    2. // It is an example of single line comment syntax
    3. document.write(“javascript comment block”);
    4. </script>

    2: Comments used after the statement.

    var num1=6;
    var num2=5;
    var num3=num1+num2; //It adds values of num1 and num2 variable
    document.write("javascript comments in html");
    document.write(num3); //prints sum of num1 and num2 variable

    JavaScript Multi line Comment

    It is used to add single or multi line comments. So, it is more convenient.

    We can use multi line  comments in our statement. Lots of programmers prefer multi line comments because it is easy to use. 

    To write this comment forward slash and asterisk(*)sign is used.

    To start the comment first add forward slash(/) then asterisk(*)sign and to end the comment add asterisk(*) sign then forward slash(/).

    For example:

    1. /* multi line comment for just one line */

    You can use it anywhere in a JavaScript statement. middle,  before, after or in the middle.


    /* It is a multi line comment. It is easy to use */
    document.write("javascript comments best practices");

    So now you can learn more.  HTML Registration Form – Easy Step by Step