Question

When you write JavaScript bookmarklets, you can't use // because the JavaScript code is treated as a single line. You have to use /* ... */ instead. This doesn't make sense to me because you can use // in normal scripts even though the newlines are ignored.

In section 2.3 of The Definitive Guide

JavaScript ignores spaces, tabs, and newlines that appear between tokens in programs...

Then, in section 2.5 of The Definitive Guide

...Any text between a // and the end of a line is treated as a comment and is ignored by JavaScript...

Those statements seem contradictory.

According to section 2.3, the normal script is pretty much a single line of J.S as well since newlines are ignored. Can someone explain to me why // is possible in a normal script, but not in a bookmarklet?

Was it helpful?

Solution

Let me emphasize part of the first quote

JavaScript ignores spaces, tabs, and newlines that appear between tokens in programs

Programs are divided into tokens. For example, the following program

var x = "abc123";

has 5 tokens, namely, var, x, =, "abc123" and ;. If you put more spaces between the tokens,

var    x     =    "abc123"       ;

it doesn't matter. But if you put a space inside of a token, then it can change the program. For example,

va r x = " a b c 12 3";

does not compile.

This difference is why the two statements are not contradictory. A comment is a single token that is ignored. If you put a newline in the middle of a comment, then you are not putting space in between tokens in a program, you are putting a space in the middle of a token.

A // comment ignores the rest of the content on the line. Since bookmarklets only contain 1 line, you would be commenting out everything after the //. In a normal script, you can have many lines, so you don't have the same problem.

If you are curious how this is specified, here's the relevant part of the grammar in the ECMAScript 5 specification:

SingleLineComment ::
   // SingleLineCommentCharsopt

SingleLineCommentChars ::
   SingleLineCommentChar SingleLineCommentCharsopt

SingleLineCommentChar ::
    SourceCharacter but not LineTerminator

OTHER TIPS

Because a bookmarklet is a single line script, where as a regular script can be many lines.

Section 2.3 is a little vague. There are cases where white space does matter but the // just tells the processor to ignore the rest of that line.

If you used // in a bookmarklet, it would just stop processing right there.

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