I don't understand which lines I am supposed to terminate with the semi-colon ;
character in JQuery.
At the moment I just terminate every line with it out of safety. But is there a rule on where and when to use it?
I don't understand which lines I am supposed to terminate with the semi-colon ;
character in JQuery.
At the moment I just terminate every line with it out of safety. But is there a rule on where and when to use it?
To begin with, JQuery is a library (Javascript library) not a language so you are asking about the usage of semicolon in Javascript.
Here is a guide in using semicolons in Javascript: http://www.codecademy.com/blog/78-your-guide-to-semicolons-in-javascript
And here is a nice conversation over the thema: Do you recommend using semicolons after every statement in JavaScript?
jQuery is just Javascript, and as such you should terminate with the semi-colon anywhere it makes sense to do so in Javascript. Namely, at the end of a statement or line.
For example:
$("#element1").hide(); // End of statement
or:
$(function() {
alert("hi!"); // End of statement
}); // End of statement
Note that the above could be written as:
$(function() { alert("hi!"); });
In this case, the outer anonymous function should end with a semi-colon (since it's a statement), and the lines inside of it should also. If I'm thinking correctly, that's really the only time it seems to get confusing.
In general, it's usually okay to err on the safe side and overuse them.
Simply, the semicolon ;
is required after each statement.
Example:
$('.selector').hide();
A function declaration though is not considered a statement
Example:
function myFunc(){
}
(no ;
required)
The semicolon ;
in JavaScript is used to separate statements, but it can be omitted if the statement is followed by a line break (or there’s only one statement in a block
).
A statement is a piece of code that tells the computer to do something.
Recommendation
you should use semicolons ;
after every statement in JavaScript.