Computer >> Computer tutorials >  >> Programming >> HTML

Are HTML comments inside script tags a best practice?


JavaScript can be implemented using JavaScript statements that are placed within the <script>... </script>

</script>.
   <script ...>
   JavaScript code
</script>

Example

HTML comment is added in the <script> tag, which is optional −

<html>
   <body>
      <script>
         <!--
            document.write("Hello World!")
         //-->
      </script>
   </body>
</html>

Added an optional HTML comment above that surrounds our JavaScript code. Yes, it is considered as a best practice, since this is to save our code from a browser that does not support JavaScript.

The comment ends with a "//-->". Here "//" signifies a comment in JavaScript, so we add that to prevent a browser from reading the end of the HTML comment as a piece of JavaScript code.