Leaving out the semicolon in JavaScript

TorgoX on 2005-09-10T00:08:05

Dear Log,

JavaScript is a relatively sane language, except for its stupid name, and except that at some point someone decided to add the "feature" of making the language forgive missing semicolons. Bad bad bad bad.

I urge everyone to avoid this "feature". The following examples should illustrate some of the lunacy that comes from leaving out semicolons:

function x (n) {
  throw
  ++n
}
x(100)
// Parses as throw(++n), so it throws an
//exception whose object is the number "101" 

~~*~~

function x (n) {
  return
  ++n
}
x(100)
// Parses as return;++n, so it returns undefined.

~~*~~

function x (n) {
  return
  (++n)
}
x(100)
// Same as above.

~~*~~

function zoink (q) { alert(q||"?"); }
function x (n) {
  return zoink
}
x(100)
// Of course, parses as return zoink, returning
//  the 'zoink' function-object.

~~*~~

function zoink (q) { alert(q||"?"); }
function x (n) {
  return zoink
  ++n
}
x(100)
// Parses as return zoink;++n;, returning
//  the 'zoink' function-object.

~~*~~

function zoink (q) { alert(q||"?"); return 'huh!' }
function x (n) {
  return zoink
  (++n)
}
x(100)
// Parses as return zoink(++n);, actually calling
/   zoink, and returning 'huh!'.

~~*~~

function zoink (q) { alert(q||"?"); return 'huh!' }
function x (n) {
  zoink
  ++n
}
x(100)
// Parses as zoink;++n;, returning undefined (without
//  having called zoink, just having fetched its
//  object-value).

~~*~~

function zoink (q) { alert(q||"?"); return 'huh!' }
function x (n) {
  zoink
  (++n)
}
x(100)
// Parses as zoink(++n), actually calling zoink,
//  and returning undefined.