Semicolons in JavaScript are optional
mislav.uniqpath.com
Semicolons in JavaScript are optional
1–10 of 39 posts
Re: Semicolons in JavaScript are optional
#2Re: Semicolons in JavaScript are optional
#3Re: Semicolons in JavaScript are optional
#4the basic message seems to be "don't do what everyone else is doing just because everyone else does it".
Which is great in your bedroom, but in a team with lots of eyes on your code the most efficient way is to use the same style, with that style being one that is easy to read and that avoids potential misunderstandings and bugs.
I can't imagine any situation in the sphere of development this article is about in which worrying about a couple of hundred bytes is more important than having a set of code that someone other than yourself could happily sit in front of.
Re: Semicolons in JavaScript are optional
#5 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. Stamp semicolons
everywhere and run it through a minifier:
var a=1;var b=2;var c=3;
Yeah um, hate to break it to you, but a minifier (and any experienced coder) would write it as "var a=1,b=2,c=3;" // after minification
if(condition){stuff()}
Wrong again. Use a better minifier. Closure advanced mode for example will remove the {}, inline functions if it makes sense and hundreds of other things. Easy solution: when a line starts with parenthesis,
prepend a semicolon to it.
;(d + e).print()
Ugly horrible hacky advice.I remember when I first saw C code (coming from BASIC) and thought similar thoughts - eugh what are all those useless semicolons they don't do anything what's the point of them etc.
One important point is that it allows you to rearrange the whitespace in your code, without changing the execution meaning of your code. Which is pretty useful in making your code beautiful and readable and avoiding bugs.
Re: Semicolons in JavaScript are optional
#6Use 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…
Also, I don't understand how adding a semicolon could help against the "stupid error" you cite. Could anyone please explain ?
Re: Semicolons in JavaScript are optional
#7"If theres a language/library feature with a few quirks, use it anyway, if the border cases aren't that usual."
Thats really bad advice. The main disadvantage is that no one will, if you combine a lot of such decisions, have all this border and special cases in mind, all the time.
Re: Semicolons in JavaScript are optional
#8if(condition) stuff()
To:
condition&&stuff()
So no, it doesn't add curly brackets and increase the size of the expression. And Google closure converts:
var a=1
var b=2
var c=3
To:
var a=1,b=2,c=3
Not what he said all minifiers do.
Re: Semicolons in JavaScript are optional
#9Use 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…
return
{
foo: 7
};
still returns undefined.Re: Semicolons in JavaScript are optional
#10Use 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…
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 ?
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 - glaringly obvious and extremely offensive.
IMHO that backeting style sucks, and shouldn't be used, at least in js.