Live data from Hacker News

Semicolons in JavaScript are optional

mislav.uniqpath.com

21–30 of 39 posts

Re: Semicolons in JavaScript are optional

#21
post #15
post #10

Earlier quoted context omitted.

I don't actually remember what closure advanced mode does. It's quite possible it does remove ";" in some instances. But really, you either need a newline or a ";" to mark the end of a statement, so the question is fairly moot - it's gonna be 1 byte either way. If you always put semicolons to mark the end of a statement, then seeing a statement without one would be like seeing a megablocks brick in your lego box - gl…

If you write one statement per line, the code is very clear, and there is no need for semicolons. It is after all possible, according to the syntax, and a minifier should be able to understand it, as well as your browser. I am certainly not against coding practices or readability. With (Q)BASIC, you could also add semicolons and have multiple statements per line, but who did ? It seems that it is the same with Javasc…

it may be 'clear' to you, but as has been pointed out, it can be ambiguous as to what the code will do. The example with a line starting with an open bracket is one such example.

Why force yourself to remember edge cases, when you can just remove that whole class of bugs by being explicit about what you mean...

Re: Semicolons in JavaScript are optional

#24
post #17

I write semicolon-less code and, in my experience, there isn't a JavaScript interpreter that can't handle it. Come visit me at work and I'll show you a few JS implementations on various set-top-boxes that will freak out if you don't use semicolons.

So in essence, the problem is still to be blamed on those specific JS implementations. Optional means optional, at which point making an implementation that demands the absence of semicolons is as bad as one explicitly requiring them, as both are breaking the set rule: optional.

(personally, I'm for the mandatory semicolon, to avoid mistakes)

Re: Semicolons in JavaScript are optional

#25
post #5

Use semicolons. Always. It's looks cleaner, and you won't come up against stupid errors like return { foo: 7 } From the article - When you're done trying to wrap your brain around why would anyone in their right mind want to write a return statement on a new line Well, if they love having { on newlines, then it's pretty obvious: function foo() { return { bar: 8 } } From the article - That's 24 bytes right there. Stam…

> It's looks cleaner, and ...

I think my new pet-peeve is the word "clean". What you mean, when you say that, is that it looks good, according to your taste. That's a rather subjective statement, disguised as an objective one.

(end of rant)

Re: Semicolons in JavaScript are optional

#27
post #25
post #5

Use semicolons. Always. It's looks cleaner, and you won't come up against stupid errors like return { foo: 7 } From the article - When you're done trying to wrap your brain around why would anyone in their right mind want to write a return statement on a new line Well, if they love having { on newlines, then it's pretty obvious: function foo() { return { bar: 8 } } From the article - That's 24 bytes right there. Stam…

> It's looks cleaner, and ... I think my new pet-peeve is the word "clean". What you mean, when you say that, is that it looks good, according to your taste. That's a rather subjective statement, disguised as an objective one. (end of rant)

Yeah point taken. I guess what I mean here is that it removes ambiguity and separates logic (What the code is going to do) from display (How I like my source code laid out).

I wasn't suggesting that languages that don't have semicolons look ugly or unclean. I do a lot of assembly language but obviously there is no real issue there with semicolon/line endings, since the convention is to write a single instruction per line.

Re: Semicolons in JavaScript are optional

#29
Do we really want to heed advice when it's comes with obviously wrong advice like "Only people can detect and solve software bugs, not tools."

Static bug analysis is definitely not the be all and end all of software quality but definitely has a place. We automatically run FindBugs (along with unit test etc) on our code base every time new code is pushed by a developer and it definitely helps pick up quirky little errors much more cheaply than code reviews.

Re: Semicolons in JavaScript are optional

#30
post #12
post #9

Earlier quoted context omitted.

I don't think semicolons help your example. return { foo: 7 }; still returns undefined.

And that's one of the reasons (IMHO) that bracketing style sucks. I was really explaining why rookies will make mistakes with js. The simplest way to prevent those types of errors is to always use ; and to use { on the same line style.

I understand the curly on the same line thing can bite you in Javascript, but man I find it so much easier to read when I can match them up by line of sight on a new line, that is one of my big pet peeves.
Post reply on HN