JRS: In article <
[email protected]>, seen in
i start with javascript and i saw a lot of examples, but sometimes there is
";" on each end of line, sometimes only certain lines, sometimes nowhere.
So my question is: where must i put ";" in a javascript (in function, out
function)?
(A) Where there are two or more separate statements on a single line,
you must use a semicolon to separate them.
(B1) At the end of a line, it is often necessary not to put a semi-
colon, because the statement is not yet complete.
(B2) At the end of a line, it is sometimes necessary to put a semi-colon
to show that the statement is complete.
(B3) Otherwise, at the end of a line, please yourself. Many people use
them whenever possible.
(B4) Note that two lines may be valid code, sometimes even meaningful
code, but giving different results, both with and without the semi-
colon. Consider
for (j=0;j<2;j++) // possible ; here
alert(j)
A programmer should know, when about to start a new line, whether the
previous statement is intended to finish. If it is, a semicolon is
permissible and perhaps desirable. If it is not, it must be evidently
incomplete, by having an unclosed bracket or a terminal operator (or
....?).
The system will insert a semicolon (or act as if it has) when it sees
fit; it will not remove one that it dislikes.