Wasted a couple of “What the hell is wrong here?” moments about this code:

1
2
3
4
5
function doSomething() {}
doSomething()
(function () {
  // self executed
})();

Runtime error: doSomething() is not a function.

Obviously, I missed a semicolon after the doSomething() call, but I didn’t imagine this caused the error.

JS usually forgives about missing semicolons, if next statement is not in the same line. However, having the parenthesis of the self executed function made JS parse code as

1
doSomething()(function (){})();

Which expects the returned value from doSomething to be a function, that is executed with the anonymous function as a first argument.

So in conclusion, never forget semicolons. Ever. Even when just debugging code.

Comments