PDA

View Full Version : Using semi-colons or NOT!



hsantos
06-16-2006, 04:14 AM
Whats the skinny with NOT using semi-colons in javascript?

I guess I am using to using semi-colons to "cleanly" seperate code, but I guess that just my style.

But I see example scripts without semi-colons and in one case, where I was studying the code, I added semi-colons, and it change (didn't behave as expected) the script. That might be something I did wrong but I didn't see it.

So whats the story? Use SEMI-COLONS or NOT?

thanks

jscheuer1
06-16-2006, 04:43 AM
Generally, in javascript code, semicolons may be added to the ends of lines and in many cases this eliminates the need for a line break in the code, saving one character (if that is important) or simply making the code clearer. However, there are many situations in which adding a semicolon to the end of a line of script will break the script. If you don't fully understand the code or don't know javascript fairly well and the code works 'as is', best to leave it alone.

Various situations can arise where a semicolon is not called for. One that leaps to mind is a train of variable declarations:


var p='px', a=0,
x='width', y='height', crossobj,
busy=false;

for example. Putting semicolons at the end of those lines without them will break the train.

Breaking up for in's and if, else if, else trains inappropriately with semicolons are two more. I'm sure there are others.

hsantos
06-16-2006, 09:14 AM
Thanks for your input. I see your point about "trains." Thats probably what happen when I was breaking up the example js and it then failed to behave right.

Probably a poor example, but how about?

var x var y

??

Without trying it myself, is this syntaxically incorrect?

I guess I am too much old school, having learned 25+ different languages in my time, where structure and clean code writing/reading "meant alot" no doubt Java/Script is pretty "democratic" about things :-)

Anyway, I appreciate your input. :)

---
HLS

jscheuer1
06-16-2006, 09:30 AM
var x var y

I think in itself that is syntactically invalid javascript. Its intent seems to be to declare x and y as variables. This can be done a number of ways:


var x
var y


var x;
var y;


var x
var y;


var x; var y;


var x, y


var x, y;


var x,
y;

etc. In this situation the semicolon and the line break are interchangeable, as long as a comma delimited train is not involved.

Twey
06-16-2006, 11:52 AM
See ECMA-262 (http://www.ecma-international.org/publications/standards/Ecma-262.htm) for the full rules of exactly where semicolons are inserted.

I tend to seperate statements with both a semicolon and a linebreak. It's just good style: it makes the code easier to read, and I figure that if the parser is going to insert semicolons anyway, I might as well get there first; it might even have a positive effect on performance.

hsantos
06-16-2006, 01:10 PM
Thanks, I'll read up on ECMA-262.

Like yourself, I use well outlined, documented clean code with semi-colons and line-breaks. Generalize functionality and reusability is big with me.

I guess its a matter of what one is use to. I will venture among the top three languages during the 80,90 and into the 2000s was BASIC, PASCAL and C/C++. BASIC does not use semi-colons. So with the JAVA/SCRIPT language becoming ever so popular, what you see, probably tells you more what language the JS programmer was more familar with in the past. So when I see semi-colon-less code, I think "must be a VB programmer" :D

---
HLS

Twey
06-16-2006, 01:24 PM
And that's enough of an insult to cause them to fill their code so full of semicolons it's hard to make out the actual text :)
the JAVA/SCRIPT languageIt's actually called ECMAScript, but commonly referred to as Javascript, which was its predecessor. I don't know if you're confusing it with Java there as the slash implies, and pardon me if you're not, but Java and ECMAScript are two totally different languages; they just happen to have similar names. Java is much more like C++ than ECMAScript.

hsantos
06-16-2006, 05:36 PM
Oh, I do not mean to start a language debate. :-) Currently, I happen to work in a mixed language product development environment with Pascal, C/C++, VB, Java for our native language SDK/API and a higher layer/wrapper with our own p-code (Wildcat!) BASIC application development language. I pretty much stayed away from JavaScripting for many years simply for various reason probably mostly because we didn't need it and for maximum browser compatibility. Currently, the Java version SDK is for our persistent web-based client interfaces. So I would be the last person on earth to enter into a language debate. :-)

But we are now fancying up the interface and WEB SDK/API with AJAX, thus working more with JavaScripting (and DOM) now. I wasn't sure if there was a technical reason or "programming preferrence" for not using semi-colons in JavaScripts.

It sounds like it just a matter of style? Nes pas?

---
HLS

Twey
06-16-2006, 05:46 PM
Oh, I do not mean to start a language debate. :-)No, I was just taking a passing snipe at Visual Basic and all its modern ilk. :)

It sounds like it just a matter of style? Nes pas?Mostly so, yes. As I said, though, the parser inserts semicolons in appropriate places before execution. Adding semicolons reduces ambiguity.

Astonishingly, I actually agree with something posted on MSDN (http://blogs.msdn.com/ericlippert/archive/2004/02/02/66334.aspx). :)

jscheuer1
06-16-2006, 06:01 PM
I like to use semicolons wherever they belong, if I'm not in a hurry. If I am in a hurry, I tend to go back later and put them in. It just makes things clearer to me, what I intend the code to do. This does occasionally result in code I write missing a few semicolons, as long as the parser can handle it.

mwinter
06-17-2006, 03:22 PM
Whats the skinny with NOT using semi-colons in javascript?They are optional. The language defines what is known as 'automatic semicolon insertion': the semicolons are there, but are implied.


I guess I am using to using semi-colons to "cleanly" seperate code, but I guess that just my style.If you're happy with it, carry on. I also include semicolons explicitly. I prefer a more verbose approach to syntax (though often with the exception of some operator trickery). Including semicolons is also absolutely necessary if one wishes to use a tool like JSMin.


But I see example scripts without semi-colonsIt is not infrequently laziness, rather than a reasoned decision to omit them.


and in one case, where I was studying the code, I added semi-colons, and it change (didn't behave as expected) the script.You probably inserted it into the wrong place. That's all.


So whats the story? Use SEMI-COLONS or NOT?I don't think you'll find a great deal of consensus. If the language was more widely debated - I don't think there are currently enough experts for that - this would probably one of those 'religious' topics that exist within other well-established languages.


var x var y

??

Without trying it myself, is this syntaxically incorrect?Yes, it is. The var keyword defines a variable declaration statement. Following the keyword should be a list of comma-separated identifiers and, optionally, initialisers for those variables. In the snippet above, three consecutive identifiers follows the keyword, one of which is a reserved word (the second occurance of 'var').


[...] no doubt Java/Script is pretty "democratic" about thingsIt isn't Java/Script. That implies some relation to Java and there is none (though the syntax and set of future reserved words might appear otherwise). Including the word 'Java' was just a marketing trick to ride on the back of Java as that latter was in its hyped-up prime at the time. The Netscape version of the language is called JavaScript (capital J, capital S). Microsoft called their implementation JScript. Other browsers and hosts have their own names. A generic reference to the language is often written Javascript or javascript (though see a little later).

Javascript is a scripting language; 4GL. Useability and flexibility (and it quite surprisingly so, once one becomes familiar with it) was a more important design goal than a convenient parser implementation.


Thanks, I'll read up on ECMA-262.Be aware that the specification (all editions) is not easy to read at first. It is a somewhat formal, and technical document, and nothing like a tutorial or easy-to-read guide. However, there will become a point where everything seems rather obvious, but it may take time. Understanding the language through other means undoubtedly helps.



It's actually called ECMAScript, but commonly referred to as Javascript, which was its predecessor.Hold on a moment. ECMAScript is the formal language specification of the most commonly implemented scripting languages in browsers. It is a generic scripting language, derived from both JavaScript and JScript. Since the time of its publication, these two implementations, along with several others (ActionScript, for example) outside the scope of browser scripting, conform (more-or-less) to one edition or another. ECMAScript simply defines a core set of objects, functions, syntax, and - most importantly - semantic behaviour. JavaScript is still JavaScript. JScript is still JScript. What is different is that they claim to conform to an independant behavioural specification.

When I refer to ECMAScript, I am making a reference to behaviour defined their, rather than what one might find in some other language specification or host implementation. All implementations have their quirks and extensions, and as such, they are still separate languages.

I've noticed that you've used the name ECMAScript (http://www.dynamicdrive.com/forums/showpost.php?p=37274&postcount=19) for other uses. One must remember that there is a boundary between the language and the host. The only thing that can be described as ECMAScript is the features defined in ECMA-262. Everything else is either part of a particular implementing language, or the host environment. An example of the former is conditional function declarations[1] in JavaScript:



if (condition) {
function foo() { /* ... */ }
} else {
function foo() { /* ... */ }
}
An example of the latter is the W3C DOM, 'DOM 0', and features like the innerHTML property.

Mike


[1] ...conditional function declarations.

Do not use that feature. It can easily be duplicated in a way that is compatible with all ECMAScript implementations:



var foo;

if (condition) {
foo = function() { /* ... */ };
} else {
foo = function() { /* ... */ };
}

Twey
06-17-2006, 03:50 PM
An example of the former is conditional function declarations[1] in javascriptTo which implementation does this belong?
I've noticed that you've used the name ECMAScript (http://www.dynamicdrive.com/forums/showpost.php?p=37274&postcount=19) for other uses.I think it's appropriate in the post to which you refer... though I'm sure I've probably misused it elsewhere. :)

mwinter
06-17-2006, 04:16 PM
An example of the former is conditional function declarations[1] in javascriptTo which implementation does this belong?I meant to write 'JavaScript'; Gecko browsers.

Mikel