Function In If Condition Clause
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 som
Solution 1:
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 herefoo(); // okbar(); // doesn't exist
So in effect you're never declaring a function x
, which is why none exists when you try to call it.
Post a Comment for "Function In If Condition Clause"