Are HTML comments inside script tags a best practice?


Before trying to understanding whether HTML comments inside script tags is best practice or not, let us discuss about how to write comments in HTML, what are the different ways to comment in HTML?

Generally, comments are helpful to understand the statement, it leaves remainders and provide explanations. It also helps in disabling the statement when we are testing or working on new feature.

Syntax

Following is the syntax for comment representation −

<!--  statement-->

Let us see different ways to represent comments in a program −

Inserting a single-line comment

The single line comment is applied to single statement with open tags and closed tags such as, <!-- -- >. We will insert these comments for reminding what is happening with the code.

Note − There should be no space in comment tags like < ! – is not valid. Inside the tag any number of spaces can be included, but tag has to be represented without space <!--.

Example

Following example shows how to represent single line comment −

<html>
<head>
   <title> Single line comment</title>
</head>
<body>
   <!—paragraph begins here -->
   <p> TutorialsPoint Website</p>
</body>
<html>

When we run the above code, a single line comment “Tutorialspoint website” will be displayed.

How to create multiline comment

Multiple lines can be span by using multiline comments, it is helpful in explaining complex codes.

Example

Following is the example, which shows how to represent multiline comment in HTML.

<html>
<head>
   <title> Multiline comments</title>
</head>
<body>
   <!—
      TutorialsPoint is having a technical content website Where we can find all technical related information with Content as well as videos 
   -->
   <p> TutorialsPoint WEBSITE </p>
</body>
<html>

When we run the above code, the output is displayed in multiline comments.

How to use comment function for quickly disable code

We can use comment function to quickly block, when we are trying to track an error or trying to keep code from running on the page like this we can easily restore the code by simply deleting the comment tag.

Example

Following is the example, that shows how to use comment function for quick disabling of code in HTML.

<html>
<head>
   <title>Comment function for quickly disable code</title>
</head>
<body>
   <p>Images related to Courses</p>
   <img src="https://www.tutorialspoint.com/images/ncert-books-class-6-maths_icon.svg" height=150 width=150><br><br>

   <!-- Hiding this image for now
   <img src="/images/pic2.jpg height=150 width=150"><br>
   -->
   <img src="https://www.tutorialspoint.com/images/statistics_icon.svg" height=150 width=150>

</body>
</html> 

When we run the above program, Images related to courses text will be displayed along with the images.

Comment function to hide scripts on unsupported browsers

If we are using JavaScript or any other scripting language, we use comment function to hide the script on browsers that doesn't support the browsers. To ensure that the script works on browser which supports we have to Insert the comment at the start of the script, and end it with //-->.

Example

Following example shows how to use comment function to hide scripts on unsupported browsers.

<html>
<head>
   <title>JavaScript</title>
</head>
<body>
   <script language="javascript" type="text/javascript">
      <!--
         document.write("Hello World!")
      //-->
   </script>
</body>
</html>

Example

Consider an example which explains whether the writing comment in HTML is a best practice or not.

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>

Updated on: 09-Nov-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements