JavaScript - Problem With Semicolon Insertion

javascript

// JavaScript - Problem with semi-colon insertion:
function foo1()
{
  return {
      bar: "hello"
  };
}
function foo2()
{
  return
  {
      bar: "hello"
  };
}
In the above code, foo1() and foo2() are almost identical except that in 
fool1(), the return line contains the curly brace, where as in foo2, the curly
brace is on a separately.  When this happens, JavaScript automatically insert
the semi-colon on the return line turning it into 'return;'.  Semicolons are 
technically optional in JavaScript, although omitting them is generally really 
bad form.  As a result, when the line containing the return statement (with 
nothing else on the line) is encountered in foo2(), a semicolon is 
automatically inserted immediately after the return statement.  No error is 
thrown since the remainder of the code is perfectly valid, even though it 
doesn’t ever get invoked or do anything (it is simply an unused code block that 
defines a property bar which is equal to the string "hello").
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License