Posted about 2 years ago
tlnt.co/p/18y

Hey! Semicolons in JS are optional! I bet most of you may have known that, but it was news to me. Are there any reasons to include semicolons, outside of bookmarklets and familiarity?

You might also like

Computer Science in JavaScript
Prototypes in JavaScript
Sprite3D.js - A simple javascript library to help making things move in 3D space

Comments

Default_thumb
Dave Everitt

I've just had a massive debate about this with a friend who is as much at home with C as with Javascript. His view is that it's good practice to include them to remove potential ambiguity. My argument was that that the edge cases appear to involve badly-formatted code - in other words, if it's well-written the semicolons will be inserted by the parser exactly where you'd expect them. Except for statements that begin with a '(' where you'd have to prepend a semicolon ';(' if you didn't add one at the end of the preceding line.

So if

function add() {
  var a = 1, b = 2
  return a + b
}

works because parsers insert semicolons where you'd expect them, why bother adding them ourselves? I don't know if I have a good answer, even after reading the various arguments for each case in this StackOverflow thread.

The best summary I've read about this issue is in the final two paragraphs ('Misconceptions' and 'Conclusion') from the aptly-named article JavaScript Semicolon Insertion - Everything you need to know: whether you choose to include or omit semicolons in Javascript, do it knowingly.

about 2 years ago   Like_icon 1 likes  
15b2f7b_thumb
Abelardo Gonzalez

If I had known this before I learned C, etc. I probably would not be inserting semicolons right now (coming straight from VB). I've gotten so used to it that it's hard to avoid it though.

You'll sometimes find semicolons instead of periods at the end of my sentences too;

about 2 years ago   Like_icon 1 likes  
Default_thumb
Dave Everitt

...just like I sometimes start lists with '0' :-)

about 2 years ago   Like_icon 1 likes  
15b2f7b_thumb
Abelardo Gonzalez

hah! I do that I work also; Confuses everyone;

about 2 years ago   Like_icon 1 likes  
Jared_gaze_thumb
Jared Brown

I write a fair amount of onclick handlers and other inline snippets of JavaScript. For those I need to use the semicolon.

Example:

onclick="alert('Hello world'); $('#aForm').submit(); return false;"

So I'm just in the habit of using them. For me I think it would have a mental cost to use them for my inline JS and not in my external files.

about 2 years ago   Like_icon 1 likes  
Default_thumb
Dave Everitt

Yes, it's logical that inline js has the same requirements as any multiple lines of code...

about 2 years ago   Like_icon 0 likes  
Newlogo_2_thumb
Tony Brown

amazing how much discussion there is around the use of semi-colons, I just use them like all the books say to. If you don't like them just be careful.For me JS is harder to read when there are no ; and if you omit the curly braces oh man lol

about 2 years ago   Like_icon 1 likes  
15b2f7b_thumb
Abelardo Gonzalez

Heh. I even use the curly braces for one-line conditionals. That really is confusing for me if I don't put it in, even with indentation. My eyes just look over it.

about 2 years ago   Like_icon 2 likes  
Newlogo_2_thumb
Tony Brown

yep :)

about 2 years ago   Like_icon 0 likes  
Img_1421_thumb
Edward Rudd

I always use semicolons to ensure there are no surprises in how the compiler rewrites code.. IMHO optional semicolons was a BAD idea for JavaScript.

http://stackoverflow.com/questions/8944398/why-does-leaving-off-a-semicolon-break-this-code

almost 2 years ago   Like_icon 2 likes  
Default_thumb
Dave Everitt

@Edward yes, if Javascript had been designed without semicolons, it wouldn't be an issue. And that example breaks exactly where you'd expect. But I might still write some js that no-one else will see without them, just as a 'proof of option'.

However, if a critical mass of js coders began to omit them knowingly as a coding style, then there'd be a whole new discussion...

almost 2 years ago   Like_icon 0 likes  
Default_thumb
Robert Davis

I usually generate bits of javascript using php so it's natural to include them in order to minimize the changes between languages. It's bad enough that I have to remember that in JS I have to use string.length instead of strlen(string) in php, for example.

almost 2 years ago   Like_icon 1 likes  
Default_thumb
Nicholas Johnson

No, semi-colons are not optional. Here's why:

If you leave your semi-colons off, the parser will attempt to execute your code as though it were all one line. It will do this until it encounters an exception. At this point it will backtrack until it finds a newline and insert a semi-colon there for you.

If you leave your semi-colons off, you force the parser to attempt to run your code twice, and deal with resultant error trapping.

In addition, there are some edge cases where two lines of code can be executed as a single line without throwing an exception, in a way that produces a different result than if the lines executed as two lines.

Always remember your semi-colons. It's good form, it makes your code faster, and it avoids hard-to-find bugs.

This according to Crockford. I'd recommend this video for all learning JS developers:

almost 2 years ago   Like_icon 2 likes  
15b2f7b_thumb
Abelardo Gonzalez

Oh, well, you put it that way. I'm pretty sure non-semicolon languages handle line breaks like JS handles semicolons and plow through your code, only recognizing two lines as one when there's a special character for that (little sideways L in hypertalk, don't know about the others).

But, if it's doubling back as it parses the code? That just makes me cringe a little.

almost 2 years ago   Like_icon 1 likes  
Img_1421_thumb
Edward Rudd

The big different there is that non-semicolon languages ALWAYS mean a line break to be a new statement.. Where as JS it, doesn't always, and can break things in non-obvious ways. Not to mention that uglifiers/JS compressors don't work well when you don't use semi-colons.

This is one of those "Just because you can, doesn't mean you should" kind of things. Douglas Crockford is very open about his distaste of the "optional semicolon" and the issues it has. See JavaScript: The World's Most Misunderstood Programming Language

almost 2 years ago   Like_icon 1 likes  
Default_thumb
Dave Everitt

@Nicholas: "If you leave your semi-colons off, you force the parser to attempt to run your code twice, and deal with resultant error trapping" - that's the only really persuasive argument so far, despite Crockford (who I've read/watched). I'm used to using them, so - given that argument (although I haven't checked it's veracity - sources?) - am unlikely to stop just on a whim. For me, that's probably the end of the discussion! Unless future parsers change...

almost 2 years ago   Like_icon 1 likes  
Jared_gaze_thumb
Jared Brown

I agree with you Dave. There's no good reason to drop semi-colons and at least several technical reasons to leave them (compressor, non-performant in the parser).

almost 2 years ago   Like_icon 1 likes  

Talentopoly Newsletter

A once-weekly round-up of the best programming and design posts.

Join 2050+ subscribers

We will never spam or share your email address. Easily unsubscribe

Liked_post_header
Default_speck Jared_gaze_speck Paradoksetdesign1252439971_speck Default_speck