Patient Guy said the following on 1/16/2006 1:05 AM:
(e-mail address removed):
http://groups.google.com/groups?q=semicolon+Javascript&hl=en
The 1st, 2nd, and 4th links in that results page show good threads
containing posts from individuals who urge the use of semicolons in
Javascript, and those who shrug their shoulders about whether they should
be required.
Let me take a guess, off the top of my head, of people who might be in
those 4 threads, in no particular order. You will have to trust my
integrity when I tell you I am making this list prior to looking at that
URL:
Richard Cornford
Douglas Crockford
Lasse Reichstein Nielsen
Thomas Lahn
RobG
Michael Winter
Not sure about Martin Honnen and Jim Ley, my memory fades with them.
People who I *know* have shrugged his shoulders at ; at the end of a
line prior to the CR/LF:
Randy Webb
But alas, I didn't ask for a list of people who "urge the use". I asked
for a real world code example where the lack of a ; prior to the CR/LF
would introduce a syntax error where adding it would prevent the error.
And to get started, I will give you three examples where adding the ;
will introduce errors and/or change the meaning of the code:
Example 1:
var j = 0;
for (i=0;i<10;i++);
{j++}
alert(j)
What does j equal? The ; changes the meaning of the code when it isn't
there:
var j = 0;
for (i=0;i<10;i++)
{j++}
alert(j)
Example 2:
function myFunction();
//syntax error, expected {
{
var j = 0;
for (i=0;i<10;i++)
{j++}
alert(j)
}
Example 3:
var myVar =
"Some text that will stretch on and on and on and in order" +
"to make code readable when viewed in an editor it is broken" +
"up across lines like I have it now."
Perfectly error-free lines of code. Now, introduce ; prior to the CR/LF:
var myVar =
"Some text that will stretch on and on and on and in order" + ;
"to make code readable when viewed in an editor it is broken" + ;
"up across lines like I have it now.";
Syntax Error.
There are three *real world examples* of code that when the line is
ended in a semi-colon it introduces a syntax error and/or changes the
meaning of the code.
What I am asking for is the reverse. An example of *real world code*
that when the ; is left off then it causes a syntax error but adding
fixes that error.
Now, I will go look at those threads.
Thread 1:
Dated from October 2001 and Jim Ley is saying:
<quote>
No it shouldn't, the compiler should insert them if need be based on
the rules of "Automatic Semi-Colon Insertion" (ECMAScript section 7.9)
</quote>
In reply to the statement:
<quote>JavaScript in the standardised version *should* have them.</quote>
Thread 2: Eric Lippert in 1998 quoting ECMA 262 and showing examples of
where the semi colon being present or not changes the meaning of the code.
He also says in that thread:
<quote>
there is no appreciable perf hit or gain for leaving out
semicolons.
</quote>
That was one thing I was curious about testing was whether having them
there made any speed difference and it doesn't seem to.
Thread 4:
Once again, Jim Ley had something to say about it:
<quote>
Automatic semi-colon insertion is a feature of the language, they will
be added in most circumstances where you would go ;<newline>,
therefore you can leave them out.
</quote>
And if I were to have to chose who to listen to: Jim Ley or Eric
Lippert, well, Eric loses.
I write Javascript pretty much the same way I used to style C, from which
Javascript ultimately derives.
Many people do write Javascript that way. And many people use JS as a
learning language to get to other languages.
The inclusion of semicolons makes code readable and maintainable by others,
maintainable? Yes, I agree with that.
more readable? No.
You are saying that this:
var myVar= myVar1 + myVar2 + myVar3;
is easier to read than this:
var myVar= myVar1 + myVar2 + myVar3
Solely because of the ; at the end of the line?
Again, I am not referring to in between script statements but at the end
of a line prior to the CR/LF.