r/javascript • • Feb 05 '11

Are Semicolons Necessary in JavaScript?

http://aresemicolonsnecessaryinjavascript.com/
0 Upvotes

66 comments sorted by

View all comments

18

u/drobilla Feb 06 '11

Nobody argues it isn't valid to omit semicolons. People recommend against it because it is a mind-blowingly braindead language "feature" that should not exist in the first place, and can cause confusing errors and other problems.

Fuzzy ambiguous syntax may have a place in content (e.g. markup), but it sure as hell has no place in programming language syntax. The tragedy of Javascript is that underneath the awful veneer is a pretty elegant language...

-2

u/sjs Feb 06 '11 edited Feb 06 '11

If you prefix ( and [ with semicolons you're good to go. Nowhere else are they needed, and the problems with ASI aren't solved by using semicolons everywhere.

e.g.

function foo() {
    return
    {
        something: 'here'
    }
}

Using semis everywhere doesn't save you from ASI, you just have to be careful about line breaks. Whether you use semis or not. The problem isn't omitting semicolons, it's inserting spurious newlines. Keep braces on the same line as returns or wrap the returned expression in parens, return (....

http://inimino.org/~inimino/blog/javascript_semicolons

http://blog.izs.me/post/2353458699/an-open-letter-to-javascript-leaders-regarding

You have to know the rules either way. It doesn't matter whether or not you use them, it's purely a style thing. Either way we shouldn't try to make anyone else conform to our preferred style unless they're contribution to a project.

edit: Downvotes but no rebuttals. If you ask me that's pretty telling.

1

u/vectorjohn Feb 07 '11

var f = function(){} var x = 9

syntax error. Downvotes explained.

1

u/sjs Feb 08 '11 edited Feb 08 '11

That's a straw man, nobody writes code like that. You should be writing it one of these ways:

var f = function(){},
    x = 9

or

var f = function(){}
var x = 9

If you want to write it on one line you obviously need a semi colon. But if anyone wrote code like that in a project I worked on I would fix it, that's just stupid unless it's something trivial like var a = 1, b = 2 which would still be only one statement.

1

u/vectorjohn Feb 08 '11

I may have misunderstood. I was thinking you were implying this code would work minified, when it wouldn't. But I see somewhere else you said that the minifier should be a proper javascript parser (the more popular ones aren't, hence the argument).

So n/m. But semicolon insertion is a mis-feature anyway. White space should either be significant or not, not sometimes significant.