How to use comments to prevent JavaScript Execution?


If you want another code while working in the same code document, then use comments. Comment the previous code and add the alternative code to test it. After the testing completes, uncomment the previous code. In this way, you can test both the codes.

While using comments follow the below rules to create valid comments −

  • Any text between a // and the end of a line is treated as a comment and is ignored by JavaScript.

  • Any text between the characters /* and */ is treated as a comment. This may span multiple lines.

  • JavaScript also recognizes the HTML comment opening sequence <!--. JavaScript treats this as a single-line comment, just as it does the // comment.

  • The HTML comment closing sequence --> is not recognized by JavaScript so it should be written as //-->.

The following example shows how to use comments in JavaScript.

<script>
   <!--
      // This is a comment. It is similar to comments in C++

      /*
         * This is a multiline comment in JavaScript
         * It is very similar to comments in C Programming
      */
   //-->
</script>

Updated on: 30-Jul-2019

199 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements