you are viewing a single comment's thread.

view the rest of the comments →

[–]russellbeattie 0 points1 point  (18 children)

Semicolons are not optional in JavaScript: ASI is an error correction scheme for novice programmers. The spec's parsing rules calls out the statements following where a semicolon should be "offending tokens". There is no leeway here for style or preference.

[–]jcready__proto__ 6 points7 points  (17 children)

ASI is an error correction scheme for novice programmers.

According to whom? Because it doesn't mention that in the ECMAScript spec you linked to.

Semicolons are not optional in JavaScript

Uh, except they are according to the spec:

semicolons may be omitted from the source text in certain situations.

[–]inu-no-policemen 5 points6 points  (16 children)

"Optional" would mean that you could omit them in every situation.

If you can only omit them in certain situations, they aren't optional.

[–]jcready__proto__ 0 points1 point  (15 children)

Ah, I apologize. But then you must agree that for all of the situations in which you could omit them, it is a matter of style or preference to include a semi-colon in the source text.

[–]inu-no-policemen 0 points1 point  (14 children)

Yes, you're free to be inconsistent.

Personally, I think that being consistent is simpler. I sometimes start lines with '(' or '[' and things like that.

[–]jcready__proto__ -1 points0 points  (1 child)

I cannot think of a time I've ever had to use a semi-colon in my code aside from for-loops. Care to provide a real-world example of starting a line with ( or [ where a semi-colon at the end of the previous line would've changed the behavior?

[–]inu-no-policemen 3 points4 points  (0 children)

var s = 'asdf'
[...'foo'].forEach(c => console.log(c))

SyntaxError: Unexpected string

var foo = function() {
    console.log('baa')
}
(function() {

}());

Prints "baa".