Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use semicolons in JavaScript? [duplicate]

Tags:

javascript

I've only written a small amount of JavaScript that runs embedded in a Java application, but it was tested using QUnit, has been mixed, and I've not noticed any problems yet.

Is there some conventional wisdom whether to use semicolons or not in JavaScript?

like image 962
quamrana Avatar asked Feb 11 '09 16:02

quamrana


People also ask

Is it better to use semicolons in JavaScript?

Now that we know that JavaScript automatically adds semicolons to certain statements with a few restrictions and rules, we can—as a best practice—use semicolons after finishing statements such as variable declaration with var, let, or const, when calling a function, using ++ or –, and when using return, break or ...

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 semicolons after IF statements in JavaScript?

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


2 Answers

Use them. Use them constantly.

It's far too easy to have something break later on because you neglected a semi-colon and it lost the whitespace which saved it before in a compression/generation/eval parse.

like image 66
annakata Avatar answered Oct 11 '22 07:10

annakata


I'd say use them all the time; most code you'll encounter uses them, and consistency is your friend.

like image 33
Hank Gay Avatar answered Oct 11 '22 07:10

Hank Gay