Question

I have been unable to find any reference to this statement in any book, manual, or site. As far as I can tell, it functions exactly as a // comment. For example:

console.log("1");
--> console.log("2");
console.log("3");

will print

1
3

What I'm curious about is exactly what the difference between --> and // is, if any exists, and also why --> seems to completely absent from all the JavaScript references I've seen - the only reason I found out about it at all was because I accidentally typed it into Adobe Dreamweaver, and it was highlighted as a comment. Thanks!

Edit: Here is a functional jsFiddle demonstrating the behavior.

Edit 2: I've tested further, and I've discovered a few things.

  • This only works if there are exactly two dashes. -> and ---> will throw errors.
  • This only works with a "greater than" symbol. --< will throw an error.
  • This will not work in the middle of or at the end of a line. console.log("1"); --> console.log("2"); will throw an error.
Was it helpful?

Solution

My guess is that the JavaScript engine, forgiving as always, is silently ignoring the line because it looks like the end of an HTML comment (<!-- -->). JavaScript inlined in HTML has historically been wrapped in an HTML comment so that browsers that don't support JavaScript would not try to parse it.


Edit:

I've done some research, and this is indeed the case.

From V8's scanner.cc:

If there is an HTML comment end '-->' at the beginning of a line (with only whitespace in front of it), we treat the rest of the line as a comment. This is in line with the way SpiderMonkey handles it.

From Rhino's Comment.java:

JavaScript effectively has five comment types:

  1. // line comments
  2. /* block comments */
  3. /** jsdoc comments */
  4. <!-- html-open line comments
  5. ^\s*--> html-close line comments

The first three should be familiar to Java programmers. JsDoc comments are really just block comments with some conventions about the formatting within the comment delimiters. Line and block comments are described in the Ecma-262 specification.

Note that the --> comment is not part of the Ecma-262 specification.

OTHER TIPS

This is not a comment. If you see it as a comment inside of code, then its probably showing as some kind of hanging chad HTML comment instead of the normal "//".

What's happening in this case is you are comparing using the greater than and then subtracting 1. Here are some examples...

var x = 3; var y = 3; console.log(x --> y);

false and x is now 2

If you place the normal comment characters "//", you'll get a syntax error (which you should)

The reason this is working as a comment is because it is returning as undefined. This is not safe to use as a comment. Oddly enough, it works fine in Chrome and you can throw all kinds of characters at it if you start the line with "-->".

--> something for nothing ~!@#@#%$#%^$&%^&*()_+_=-=[]\];'\||?>;';;; alert('test'),,143187132458

However, within IE 11, it is always a syntax error. Very cool find

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top