JS: HTML-Like Comment

By Xah Lee. Date: . Last updated: .

HTML-Like Comment in JavaScript

Browser implementation of JavaScript supports html-like comment. Lines starting with

<!--

are ignored

and lines that's just

-->

also ignored.

they are ignored in browser, but not necessarily in other JavaScript implementation. For example, its a syntax error in deno.

// JavaScript in browser
// line starting with <!-- are ignored
// line that's just --> also ignored

console.log(
 3,
 <!-- xyz
 4
-->
 );

// prints 3 4

This is noted in JS2015 spec at Appendex B ECMAScript 2015 Β§Annex B#sec-html-like-comments

History of the HTML comment in JavaScript

the reason JavaScript support part of the HTML Comment syntax is that, back in 1996 when Netscape browser invented JavaScript, many other browsers do not support it yet. So, in the script tag, people add HTML comment so that the JavaScript code won't coke browsers, like this:

<script language="JavaScript">
<!--
document.write("<p>how are you?<\/p>");
-->
</script>

the backslash in the closing p tag was to prevent browser interpreting it.

BUY Ξ£JS JavaScript in Depth