JavaScript: Why to put semicolons after each expression
January 28, 2011 Leave a comment
By JavaScript standard it is not obligatory to put semicolons after each expression when there is only one expression on a line. Parser automaticaly puts it after line. For example:
var a, b a = 7 b = 8
will be parse it as:
var a, b; a = 7; b = 8;
But not putting semicolons is not considered a good coding style and therefore sounds question: “why to put semicolons if it is not obligatory?”. The answer is that there are some cases that not putting semicolon breaks code execution flow or even worse – there is compilation error. I do not speak when there are two or more expressions, I mean:
return true
this will be parsed as:
return; true;
and undefined will be returned instead of true. Compilation error occurs, when jumping or breaking a label:
break label
These are too simple examples, but there is shown why to put semicolons after each expression.