Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Lint claims extra semicolon is bad (after `if`)

Tags:

jslint

vim

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.

like image 766
puk Avatar asked Oct 28 '11 10:10

puk


People also ask

Do you need semicolons after IF statements in JavaScript?

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.

Does JavaScript care about semicolons?

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.

Is it necessary to use semicolons in JavaScript even if there are multiple statements in one line?

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.

Do you need semicolon after curly braces JavaScript?

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).


1 Answers

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?.

like image 162
sudocode Avatar answered Sep 19 '22 01:09

sudocode