JavaScript Semicolons Are Not Necessary

Did you know that when you are writing JavaScript, most of the time you don’t actually need to use semicolon in the end of the line. In most programming languages semicolons are mandatory in the end of the line. But in more modern languages, like JavaScript, Python or Swift, this tradition has been cut off. These languages handle the the ending of a statement some other way.

In JavaScript a line end is recognized as a ending for a statement. There are few special cases when this doesn’t happen and we will go through these in a bit.

JavaScript semicolons are however a possibility which you can use. This makes is a bit harder to understand when you need them and when you don’t. But in my opinion it is better learn when you need them and when you don’t need them. That’s the way how you learn how JavaScript actually works and why it works that way.

Most JavaScript programmers tend to always insert semicolon in the end of the line. This is mostly a habit and not an actual logical outcome. It is very rare that you should encounter a situation where code breaks or has unwanted behavior because you didn’t append a semicolon. There are some situations where this might happen and by learning them you can master JavaScript even better.

 

JavaScript Semicolon

A new line ( \n ) basically ends a statement. The following are the situations where this does not happen.

Open declaration

If you have an open declaration the new line won’t matter, for example:

Increment

If you write

It will be translated into

One line if

If you write

You won’t see the console.log statement. You need to have a statement or a semicolon to end the if.

Loops

In loops the semicolon is often necessary. For loop requires you to have semicolons as a delimiter of its parameters.

Next statement starts with an binary operator

If the next statement right after the line without a semicolon starts with one the following special operators, it will be recognized as an expression to the previous statement.

  • [
  • (
  • +
  • *
  • /
  • ,
  • .

For example:

The array will be interpreted as an expression to the previous function there will can be unwanted behavior or breaking of code. This can be prevented by using the semicolon on the right spot.

Now if you notice we prepended the array with a semicolon and everything works perfectly. This semicolon is also very noticeable because it is prepended so it kind of shines in there saying there is something going on here.

Conclusion

I recommend you all to try writing JavaScript without semicolons as it really makes you write better code. It is far more important to understand to understand the structures of JavaScript programming language than blindly append semicolons everywhere just to avoid an occasional error.

You can now leave the semicolon in peace.

  • Alex

    Perhaps not needed but some compilers require them to terminate. Secondly it’s more readable when using semi colons. Thirdly, it’s one character, what’s the problem with putting it in, unless of course you’re a hipster and then it’s probaly ironic to put them in… ?

    • Hey thanks for the comment! I wonder what compilers require them?

      I would argue against readability, certainly a subjective matter and more question about taste I guess.

      The problem is not putting it in there and yes it would be very easy. But what I’m going after is that you don’t put them in there, just to put them in there. You can use them if you want, but I’m sure quite many people want to know why they are putting them in there.