I have Javascript Lint set up to carry out syntax checking in vim, and when I have a statement such as
if (i > 0){
i--;
};
It generates the following warning
test.js|160 warning| empty statement or extra semicolon
I thought that it is best to always end statements with semicolons (see here). It's not issuing an error, but why the warning? How can I change this. I don't countless warnings when I am looking for legitimate warnings.
Semicolons come after if or else in JavaScript. They would belong after statements inside if or else blocks but they are technically optional. if blocks start with if and contain a statement block which is either one expression or { + one or more expressions + } . else blocks work the same way.
Semicolons are an essential part of JavaScript code. They are read and used by the compiler to distinguish between separate statements so that statements do not leak into other parts of the code.
This is all possible because JavaScript does not strictly require semicolons. When there is a place where a semicolon is needed, it adds it behind the scenes. This is called Automatic Semicolon Insertion.
You shouldn't put a semicolon after a closing curly bracket } . The only exceptions are assignment statements, such as var obj = {}; , see above. It won't harm to put a semicolon after the { } of an if statement (it will be ignored, and you might see a warning that it's unnecessary).
Guess it's complaining about the final semicolon after your closing brace.
};
In any programming language I have used, it is not normal to close blocks with semicolons. The block is closed by the closing brace.
There's more discussion on JavaScript: When should I use a semicolon after curly braces?.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With