1

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();
VLAZ
29.6k9 gold badges65 silver badges88 bronze badges
asked Aug 3, 2022 at 6:12
4

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.