1

<!-- is a valid comment delimiter in JavaScript, in Web browsers.

But is --> also a valid comment delimiter in JavaScript, in Web browsers?

--> console.log('is this commented out?')

This is the relevant grammar, but I find it difficult to parse.

enter image description here

Edit: I now think it is a valid single line comment, per the information in this answer, but the information in that answer still leaves room for ambiguity for me, so I am leaving this question open.

8
  • 2
    HTML comments start with <!-- and end with -->, what exactly is your question Commented Apr 6, 2020 at 21:03
  • 2
    <!-- begins a comment and --> ends a comment. There is no such thing as a "single line comment delimiter". See See stackoverflow.com/questions/3757051/… Commented Apr 6, 2020 at 21:03
  • 2
    @Pointy this is a question about JS comments, not HTML comments Commented Apr 6, 2020 at 21:05
  • @Hamms how do you know from what's in the OP? Commented Apr 6, 2020 at 21:06
  • 3
    OP linked to an ECMAScript spec Commented Apr 6, 2020 at 21:06

1 Answer 1

2

My interpretation of this is that a line like --> comment will in fact work as a comment.

This can be interpreted as the string --> followed by a SingleLineCommentChars sequence, which fits the definition of an HTMLCloseComment. An HTMLCloseComment all by itself on a single line fits the definition of a SingleLineHTMLCloseComment, which is one of the valid forms of a Comment

This can be verified in ECMAScript parsers like esprima. An input of

<!-- open --> close // standard 

produces:

{ "type": "Program", "body": [], "sourceType": "script", "leadingComments": [ { "type": "Line", "value": " open", "range": [ 0, 9 ] }, { "type": "Line", "value": " close", "range": [ 10, 19 ] }, { "type": "Line", "value": " standard", "range": [ 20, 31 ] } ] } 

Of course, just because you can doesn't mean you should, particularly in this situation.

Sign up to request clarification or add additional context in comments.

2 Comments

I guess they designed it that way to be really forgiving incase someone left some code inside a <script> after a closing -->.
that seems likely!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.