Although it looks simple JavaScript is actually very complex and full of small nuances and tricks. Sun Tzu says, “know your enemy” and if you use JavaScript you should know it very well.

In Javascript Code Style is not open for discussion, using semi columns or having the curly braces on the next line is not a matter of preference it is a language imposition and if you disregard it you might como across very hard to fix bugs

SemiColon

As you certainly know the JavaScript interpreter does not enforce the use of semicolons, but is should!

The interpreter relies on semicolons to delimit statements and they will be added when needed by the interpreter. You might be thinking “Cool, that saves a lot of typing!” but let me explain you how it works internally.

The interpreter has no way to know where the statment ends, so it will happily execute it until it encounters an error due to a missing semicolon. When this happens it will insert a semicolon and try again.

As you can see executing code statements twice is not ver performant but you can encounter weirder bugs to to missing semicolon

consider this code

var x = '123A'

("" + x).toLowerCase()

This code should convert a variable of unknown type to a string and lowercase it, instead it throws a syntax error this is because without the semicolon the code is evaluated as

var x = '123A'("" + x).toLowerCase()

Do you see the problem ?

Please always end your statements with a semicolon

Curly Braces

For the same reasons as before the placement of the opening curly brace is also not optional.

The opening curly brace should be on the same line as the statement otherwise you will be giving extra work to the interpreter