Why you should use ; in javascript

Idan Mor (sidanmor)
sidanmor
Published in
1 min readNov 29, 2016

--

Here is why:

Because it makes it clearer to someone maintaining the code later what you intend.

Because not all code maintainers understand the rules for automatic insertion well enough to maintain code with them left out.

And because JavaScript does nasty things to you when it guesses where to put semicolons. It’s better to be explicit and let the interpreter know exactly what you meant than it is to let the idiot box guess on your behalf.

JavaScript adds semi-colons if they are omitted, but it can actually mess up your code and cause strange errors.

Here are some cases:

As Douglas Crockford suggests -

Put a ; (semicolon) at the end of every simple statement. Note that an assignment statement which is assigning a function literal or object literal is still an assignment statement and must end with a semicolon.

References:

Stackoverflow

--

--

Full-Stack Team Leader, Senior Full-Stack Software Engineer and Javascript Ninja