How specifically does JavaScript understand the construct <!--
? From the point of view of JavaScript, is it yet another comment in addition to //
and /* */
?
From testing it seems that JavaSript treats <!--
like //
: a one-liner
<script> <!-- alert('hi') //--> </script>
does nothing, while
<script> <!--
alert('hi') //--> </script>
works as expected.
Where is this behavior documented?
This is not a duplicate of other questions: I don't ask why or whether or how it should be used. I ask what syntax and semantics it has in JavaScript, formally. The question is non-trivial and is not answered in other questions: for example, the behavior indicated above cannot be guessed from the other questions and their answers (in fact this was my motivation: my program with a one-liner as above did not work, and those questions and answers were no help in understanding why).
Single Line Comments Single line Javascript comments start with two forward slashes (//). All text after the two forward slashes until the end of a line makes up a comment, even when there are forward slashes in the commented text.
The only HTML comment is <! -- --> . It can be used as a single line comment or double; it is really up to the developer. So, an HTML comment starts with <!
So when javascript was first released, a technique was needed to hide the code from older browsers so they wouldn't show it as text in the page. The 'hack' was to use HTML comments within the script block to hide the code.
To make a single line CSS comment, put the comment text or code between /* */ tags.
From testing it seems that JavaSript treats
<!--
like//
a one-liner
Yes it does, as specified in the ES6 spec, annex B:
B.1.3 HTML-like Comments
Comment :: MultiLineComment SingleLineComment SingleLineHTMLOpenComment SingleLineHTMLCloseComment SingleLineDelimitedComment SingleLineHTMLOpenComment :: <!-- SingleLineCommentCharsopt
However, note the description of annex B:
This annex describes various legacy features and other characteristics of web browser based ECMAScript implementations. All of the language features and behaviours specified in this annex have one or more undesirable characteristics and in the absence of legacy usage would be removed from this specification. However, the usage of these features by large numbers of existing web pages means that web browsers must continue to support them. The specifications in this annex defined the requirements for interoperable implementations of these legacy features.
These features are not considered part of the core ECMAScript language. Programmers should not use or assume the existence of these features and behaviours when writing new ECMAScript code. ECMAScript implementations are discouraged from implementing these features unless the implementation is part of a web browser or is required to run the same legacy ECMAScript code that web browsers encounter.
So, this part only exists to describe existing "unofficial" behavior and as soon as browsers stop implementing this behavior, will be removed from the spec.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With