I tend to be a prolific user of semicolons in my JavaScript:
var x = 1;
var y = 2;
if (x==y){do something};
I recently noticed that I was looki
you can write multiple statement within the function with help of semicolon; semicolon used to separate the statement. easy to understand.
The C language uses semicolons at the end of statements, and also uses braces.
Apart from code readability, the use of semi-colons comes into picture when you actually try to minify your code. After minification, if there are no semicolons in your code, javascript engine might fail to know which statement ends where as there will be no longer be any new lines in the code.
JavaScript requires semicolons. But it does have Automatic Semi-colon Insertion.
The majority of JavaScript programmers are smarter than ASI and prefer to be explicit with their semi colons.
This is probably going to remain a debatable question as to the origin of how this this manner of coding came to be, but it could have come from the ability to differentiate between 1 and many statements within an if conditional, which is common in most languages. And also, people could have confused syntax with JavaScript's object literal
First, you have the single statement if:
if (someNumber == 2)
doSomething();
else
doSomethingElse();
Then, you have the multi-statement if (no semi-colon needed):
if (someNumber == 3) {
doThisFirst();
doThisSecond();
} else
doSomethingElse();
This syntax could also be confused with JavaScript's object literal, which does need a semi-color:
var SomeObjectLiteral = {
property: 2,
doSomething: function() {
}
};
Basically, the semi-colon is important in Javascript to determine where a command, or definition, ends. And Braces are just as important to determine blocks of these commands. But, you can have as many semi-colons as you want. If in doubt, use a semi-colon, but be prepared if someone calls you out for not needing one :)
Semicolons mark the end of statements in JS, but I do not put semicolons after a close-brace, only after a statement within a brace and ALWAYS on a return statement.
In your example I dont find it necessary.