Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does --> do in JavaScript?

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.
like image 402
Hydrothermal Avatar asked Feb 16 '14 05:02

Hydrothermal


People also ask

What does => mean in JavaScript?

It's a new feature that introduced in ES6 and is called arrow function. The left part denotes the input of a function and the right part the output of that function.

What does '!' Mean in JavaScript?

The ! symbol is used to indicate whether the expression defined is false or not. For example, !( 5==4) would return true , since 5 is not equal to 4. The equivalent in English would be not .


1 Answers

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.

like image 142
tom Avatar answered Sep 24 '22 15:09

tom