This page looks best with JavaScript enabled

Semicolon is Safer

 ·   ·  ☕ 3 min read

Javascript has and always have people who fight about anything and everything. I am always on the one side or the other, trying to raise the pitch whenever I can.

After all, what is life if there is no stupidity? We should get annoyed, irritated, angered and humiliated at every instance possible.

But, this topic is different.

This topic is about using a semicolon in Javascript. And, there is only one way shown by God or your belief system.

Use the darn semicolon.

Anyone who does not believe in that is either pure evil, or folks who like a practical joke. The latter party secretly monitors GitHub public repos to collect all bugs that are caused by lack of the silly semicolon.

What do the rules say?

Use semicolon.

They also say “the system does not need semicolon per se but it may not be intelligent enough to figure out all the right instances”. There is an entire section dedicated to what Javascript will assume, will do, and why you should use semicolons.

The Javascript engine can assume a few things in the below cases and try to pay smarter by assuming an end of statement (or semicolon).

  1. return, break, continue, throw statements in one line

  2. closure of a block ('}')

  3. end of file

  4. next line is logically different from the current line

While most understand 1-3, 4 can cause all hell loose. Some people - they never get it.

The above intelligence does not mean the engine is trying to take over your work. You have to put the damn thing in the code file to not test the limits each and every damn time.

What are the potential catastrophes if we miss semicolon?

1. Addition of unneeded characters

while(checkSum() > 0); // next statement will be executed if there is no semicolon


while(checkSum() > 0){}; //added brackers to avoid semicolon.

2. Unexpected results

a = b + c
(d + e).print()

This is equivalent to -


Similarly -

let a = 1;
[c, d].foreach(function(e) { console.log(e) })

.. and.. the below code assigns zero to the func.

var a = 0;
var func = function(x) { return x }

.. further.. For whatever crazy reason if you write the belowcode, the engine is not aligned to your creative brain.


The engine assumes assumes -


.. further more..

 a + b

becomes -

a + b;

3. Unexpected errors

if (a > b)
else c = d

This will fail with a syntax error since the engine cannot assume semicolon after the if statement - that would result in an empty statement, which it cannot permit.

In Conclusion

Who is laughing now about saving a single keystroke, punk?

Stay in touch!
Share on

Prashanth Krishnamurthy
Prashanth Krishnamurthy
Technologist | Creator of Things