Invoking function declaration in JavaScript

47 Views Asked by At

I have a doubt regarding how the function declaration are invoked in JavaScript.

I have read somewhere that function declaration can be accessed anywhere within the function it was declared on.

Lets say I declare a function within a block of code so it will be accessible anywhere or maybe outside the block as well.

But when I try to invoke the function before the block of code, I get an TypeError. But this error is not happening when I invoke the function after the block of code. Please explain why the function is not getting invoked before the block of code.

function globalFunc() {
  //..
  // ..
  funcName(); // not accessible - TypeError

  {
    function funcName() {
      console.log("Hey");
    }
  }

  funcName(); // accessible
}

globalFunc();

0

There are 0 best solutions below