Does it make sense to use HTML comments on blocks of JavaScript?


No, it is not recommended to use HTML comments to comment blocks of code. When JavaScript has first released some browsers didn’t have any support to understand the script. So a technique was then needed to hide the script code for older browsers. So that time the HTML comment within the JavaScript block was used to prevent the older browsers from showing the script code as text on the page. This technique was followed in the 1990s. Such browsers do not exist now, which wanted you to use HTML comments in JavaScript. Now all browsers understand the JavaScript blocks, so it does not make sense to use HTML comments on blocks of JavaScript.

Why do We Use Comments?

Comments are used to show the message in a meaningful way. Comments are used to explain the code and give information about the warnings, suggestions, and code so that anyone working on that code can easily understand the technical flow.

What are HTML Comments?

In an HTML file, if we want to comment on any code, we will use the syntax as

Syntax

<!— your comment/ code here -->

If the browser doesn’t support JavaScript, the commented code is not displayed or not visible to the end user.

Add HTML comment in JavaScript Block.

We could add HTML within a script block to hide the script code in browsers not supporting JavaScript. We could follow the below syntax for this.

Syntax

<script type="text/javascript">
<!--
   Some JavaScript code
//-->
</script>

We should add // before the closing tag -->

Why HTML Comments are not Recommended?

Now, we will see why HTML comments are not recommended in JavaScript code.

In the earlier days (1995's), some browsers like Netscape 1 did not have support for script tags. So when JavaScript was first released, it was needed to hide the code from older browsers so they wouldn't show it as text on the page. So HTML comments are used within the script block to hide the code.

But now, no browsers are ignorant of the <script> tag, so hiding JavaScript code is not required. Anyway, it is not good when we use XHTML documents. In XHTML documents, the source will be hidden from all browsers and rendered useless and If we have any decrement operators in our code, then -- HTML comments are not valid.

Example 1

Let's take an example with a commenting block of JavaScript in code using HTML comments.

<html> <title>Online JavaScript Editor</title> <head> </head> <body> <h2>Checking comments in JavaScript code</h2> <script> <!-- var a = 10; var b = 20; document.write(a+b); //--> </script> </body> </html>

See, we even commented on blocks of code to skip the code while execution, but it is not ignored by the engine. Because we have used HTML comments. If our browser doesn’t support JavaScript, then the script code will be treated as a comment.

Example 2

Instead of HTML comments, we can use multi-line comments to skip that code. Like,

<html> <title>Online JavaScript Editor</title> <head> </head> <body> <h2>Checking comments in JavaScript code</h2> <script> /* var a = 10; var b = 20; document.write(a+b); */ </script> </body> </html>

See, code is ignored by the engine.

If you want to write more lines of JavaScript code and want to use comments to explain the code. It is recommended to use external JavaScript files and import those files in HTML code in the script tags.

Updated on: 26-Aug-2022

74 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements