newbe: where to put ";" in a script?

A

adrien

Hi,

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

thanks for help
ad
 
D

Douglas Crockford

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

Put ';' at the end of all non-block statements. jslint will help you to place
them properly.

http://www.crockford.com/javascript/lint.html
 
D

Dr John Stockton

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

John G Harris

(A) Where there are two or more separate statements on a single line,
you must use a semicolon to separate them.
<snip>

If you'd bothered to RTFM you wouldn't have written such misleading
advice.

In C-like languages *some* kinds of statement end with a semicolon
(which in javascript can be omitted in some circumstances).
e.g. a = 5; ++a;
Other kinds don't end with a semicolon.
e.g. { }

The rule is different in Pascal-like languages where the semicolon is
not part of the statement.

John
 
D

Douglas Crockford

(A) Where there are two or more separate statements on a single line,
If you'd bothered to RTFM you wouldn't have written such misleading
advice.

In C-like languages *some* kinds of statement end with a semicolon
(which in javascript can be omitted in some circumstances).
e.g. a = 5; ++a;
Other kinds don't end with a semicolon.
e.g. { }

The rule is different in Pascal-like languages where the semicolon is
not part of the statement.

I think you are being unfair here. JavaScript has a semicolon insertion
mechanism which attempts to correct syntax errors by replacing linefeeds with
semicolons. I think this was a bad idea. I don't trust it. I think that relying
on it is strictly unprofessional. That said,

Semicolon insertion does not work in the middle of a line. So if you put two
statements on the same line, you must use the semicolon to separate them. That
can look Pascal-like. That said,

I think it is bad to put two statements on one line. It impairs readability and
increases the likelihood of editing mistakes.

http://www.crockford.com/javascript/lint.html
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
news:comp.lang.javascript said:
<snip>

If you'd bothered to RTFM you wouldn't have written such misleading
advice.

In C-like languages *some* kinds of statement end with a semicolon
(which in javascript can be omitted in some circumstances).
e.g. a = 5; ++a;
Other kinds don't end with a semicolon.
e.g. { }

Please show a circumstance where there are two or more separate
statements on a single line, but a semicolon is not necessary to
separate them.
 
L

Lasse Reichstein Nielsen

Dr John Stockton said:
Please show a circumstance where there are two or more separate
statements on a single line, but a semicolon is not necessary to
separate them.

if(x==4){x=2}y=3

A *reasonable* example would be harder.
/L
 
J

John G Harris

Semicolon insertion does not work in the middle of a line. So if you put two
statements on the same line, you must use the semicolon to separate them. That
can look Pascal-like.

I think you've misunderstood my point. I'm pointing out that some kinds
of statement do not end in a semicolon, so the semicolon rules don't
apply to them. For instance,
{ } ++a;
is a perfectly valid line containing two statements.
That said,
I think it is bad to put two statements on one line. It impairs readability and
increases the likelihood of editing mistakes.

I agree with you in general, but there are special cases where I think
one line is better :
<BODY onload="a=true; b=2;">

John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,077
Messages
2,570,566
Members
47,202
Latest member
misc.

Latest Threads

Top