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.

Ben Aston
  • 53,718
  • 65
  • 205
  • 331

1 Answers1

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.

Hamms
  • 5,016
  • 21
  • 28