r/javascript • • Feb 05 '11

Are Semicolons Necessary in JavaScript?

http://aresemicolonsnecessaryinjavascript.com/
0 Upvotes

66 comments sorted by

View all comments

4

u/australasia Feb 06 '11

An example that does unexpected things without semicolons:

var a = myFunc
(someBool ? someFunc : someOtherFunc).apply(...

myFunc will be called with the value from the tertiary as the first argument.

This is a little contrived, but there care cases where you want to start a line with parenthesis, and it really sucks to try to figure out why it's not working.

1

u/sjs Feb 13 '11

This is the only example. Lines starting with ( and [ need a semicolon at the beginning. That's the only rule you need to know to omit semicolons.

1

u/australasia Feb 15 '11

It's not the only example, lines starting with a regex also usually need a semicolon, as well as a host of other things like:

+userInput < 10 ? a() : b(); // '+' to cast to string to number

1

u/sjs Feb 15 '11

You're right, other unary operators such as ++ and -- need them as well (when used in prefix position). Less common in practice but I stand corrected nonetheless.