Live data from Hacker News

Semicolons in JavaScript are optional

mislav.uniqpath.com

11–20 of 39 posts

Re: Semicolons in JavaScript are optional

#11
Simple question: Which code does preserve your lifetime? Highly optimized, less readable but really short code, or highly readable, good structured, better maintainable code? As for me, I really enjoy sharing my code with my colleagues. Maybe there are others who don't, but they always will have to work alone in the basement where light never reaches theirs pale faces (think of 'The IT Crowd', for example).

Re: Semicolons in JavaScript are optional

#12
post #9
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…

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.

Re: Semicolons in JavaScript are optional

#13
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.

If I could upvote this comment more I would

Re: Semicolons in JavaScript are optional

#15
post #10
post #6

Earlier quoted context omitted.

The original argument is not about the minifier, but about semicolons. By the way, why would your better minifier introduce a semicolon here ? This is obviously a useless character in this example. Also, I don't understand how adding a semicolon could help against the "stupid error" you cite. Could anyone please explain ?

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 Javascript, semicolons are separators between statements, as well as newlines : why write both ? just to be sure they are well separated ?

Re: Semicolons in JavaScript are optional

#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.

Re: Semicolons in JavaScript are optional

#20
FWIW If you're using JavaScript in PhotoShop the following produces an error because there's no semicolon after the second statement:

  for(i=10;i--) { /* do stuff */ }
So maybe that's an example of a JS implementation where it differs on optional semicolons.
Post reply on HN