I have been given this function to predict the output. It's says refrence error,i am still wondering why?
if(function x(){console.log("ABC");})
{
x();
}
Hope somebody can throw some light on the lexical scope. Thanks in advance.
I have been given this function to predict the output. It's says refrence error,i am still wondering why?
if(function x(){console.log("ABC");})
{
x();
}
Hope somebody can throw some light on the lexical scope. Thanks in advance.
What you have there is a function expression, even if a named one, and you're not assigning that expression to anything. The fact that you're naming it x
doesn't mean a function x
will be hoisted in the scope, because that doesn't work for expressions.
function foo() {} // function declaration
(function bar() {}); // named function expression, the () makes it not-a-statement here
foo(); // ok
bar(); // doesn't exist
So in effect you're never declaring a function x
, which is why none exists when you try to call it.