Ad

Comments in JavaScript

JavaScript comments are the statements that are not executed and are ignored by the browser. Any statements written as comments will not be visible to the user, however, in the view source, comments are visible. It indicates the purpose of the code so that it is easily understood even if it is checked later on. Comments are used in the code to explain overall architecture (documenting) of the code and to explain function usage. Comments are short and meaningful.

In JavaScript, there are 2 types of comments:

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

Single-line comment: starts with //. Any text that starts with // (double slash) will be considered as a single-line comment, it automatically ends after the line ends.

<script>
    //This is a single-line comment, which tells about code in the function
    function message(){
        document.write("A function that displays a message")
    }
    message()
</script>

Multi-line comment: starts with /* and ends with */. It can go on multiple lines.

<script>
    /* This is a multiline comment
    * that tells about the function area
    * that takes 2 arguments.
    */
    function area(l, b) {
        document.write("Code to find area")
    }
    area(10,20)
</script>

Any number of comments can be placed anywhere in the program.

JavaScript Feedback, Questions, Suggestions, Discussion.

Ad: