HTML Comments <! - act as a single line comment in JavaScript and why?
How exactly does JavaScript understand the <!-- ? From a JavaScript perspective, this is another comment in addition to // and /* */ ?
From testing, it seems that JavaSript treats <!-- as // : single-line
<script> <!-- alert('hi') //--> </script> does nothing but
<script> <!-- alert('hi') //--> </script> works as expected.
Where is this behavior documented?
This is not a duplicate of other questions: I do not ask why or how and how to use it. I ask what syntax and semantics it has in JavaScript, formally. The question is non-trivial and does not answer in other questions: for example, the behavior described above cannot be guessed from other questions and their answers (in fact, it was my motivation: my program with one liner, as mentioned above, is not work, and these questions and answers do not helped to understand why).
From testing, it seems that JavaSript treats
<!--as//single-line
Yes, as specified in the ES6 specification, Appendix B :
B.1.3 Comments Similar to HTML
Comment :: MultiLineComment SingleLineComment SingleLineHTMLOpenComment SingleLineHTMLCloseComment SingleLineDelimitedComment SingleLineHTMLOpenComment :: <!-- SingleLineCommentCharsopt
However, pay attention to the description of Appendix B:
This appendix describes various obsolete features and other characteristics of web browser based ECMAScript implementations. All language functions and behavior specified in this application have one or more undesirable characteristics and, if there is no use of obsolete data, will be removed from this specification. However, using these features with a large number of existing web pages means that web browsers must continue to support them. The specifications in this appendix defined requirements for compatible implementations of these deprecated features.
These functions are not considered part of the core ECMAScript language. Programmers should not use or assume the existence of these features and behavior when writing new ECMAScript code. ECMAScript implementations are not recommended to perform these functions unless the implementation is part of a web browser or required to run the same legacy ECMAScript code encountered by web browsers.
So, this part exists only to describe the existing "unofficial" behavior, and as soon as the browser ceases to implement this behavior, it will be removed from the specification.