Why can I use a function before it’s defined in JavaScript?

This code always works, even in different browsers:

function fooCheck() {
  alert(internalFoo()); // We are using internalFoo() here...

  return internalFoo(); // And here, even though it has not been defined...

  function internalFoo() { return true; } //...until here!
}

fooCheck();

I could not find a single reference to why it should work, though.
I first saw this in John Resig’s presentation note, but it was only mentioned. There’s no explanation there or anywhere for that matter.

Could someone please enlighten me?

7 Answers
7

Leave a Comment