[Javascript] line end character ;

liorean liorean at f2o.org
Tue Jun 22 11:37:56 CDT 2004


Mike Dougherty wrote:
> Should external script files use the line end character ; as a rule?

The don't need it, but as a programmer the recommendation is to never leave it out.
In fact, it's considered bad practice to rely on implicit behaviors of any kind. 

> I left it off, now the javascript code cruncher(s) expect to find it

That's because it would be much more job for them to insert semicolons with 100% specificity and 100% sensitivity both. For instance, this is a single simple statement, in fact a single call expression:
----<>----
window

 /*comment
                                   comment*/.


alert

(


// blah...

"bad code!"

                    )
----</>----
While these are two complex statements, where the single semicolon terminates neither (it terminates a simple statement that is a part of one of the complex statements):
----<>----
while(true){break}do break;while(true)
----</>----

Now, making a cruncher that handles those cases, and the more complex cases that may appear, means that you effectively need to write an entire tokeniser for JavaScript just for that cruncher, something that I don't think very many are prepared to do. Especially since we have special cases such as the return statement, where you may not insert a line break between the return keyword and the return value, unlike most other statements.
 
> If the right thing to do is end lines with ";" then why isn't it required?

Because JavaScript is made to appeal to three camps:
 - Procedural and imperative (Pacal/C/C++/Java) programmers, having a very similar syntax.
 - Structured and functional (Scheme/LISP/ML/Haskell/APL/J/K/Python) programmers, having many features from both paradigms, but in general having a syntax without the syntactic explicitness of the first group of languages.
 - HTML coders, that are not really programmers and therefore are helped by laxer syntax rules. (Remember that JavaScript comes out of the same company that created the current tagsoup hell in the first place, in a time when tagsoup was considered to be absolutely optimal.)
 
Semicolon insertion in itself is not bad. A parser may do it as a feature. It was a bad feature to make mandatory in the standard, however, a bad design decision on both the behalf of Netscape and ECMA. 

> I don't think developers should be allowed (by the language) to be lazy 
> with regards to syntax, we're lazy enough everywhere else :)

ECMAScript 4 will not make it much easier for you, but you have one very nice feature in it: strict parsing mode. Have a look at the Mozilla JavaScript Language resources <http://www.mozilla.org/js/language/> if you want to see what ECMAScript 4 will look like.
-- 
David "liorean" Andersson

ViewStyles, ViewScripts, SwitchStyles and GraphicsInfo bookmarklets:
<http://liorean.web-graphics.com/>
Hangouts:
<http://codingforums.com/> <http://yourmusicforums.com/>



More information about the Javascript mailing list