Function raising inside conditional expressions

I understand how javascript goes up, functions go up before variables, and only declarations go up. But when I came across a rise inside if / else conditional expressions like this:

foo(); // "b is the output" var a = true; if (a) { function foo() { console.log("a"); } } else { function foo() { console.log("b"); } } 

Now the condition is true, therefore, according to the if block, there should have been an exit, but due to some kind of recovery, I assume that b is the exit.

So how b exit?

0
javascript hoisting if-statement
source share
2 answers

(Ignoring some tricky behavior some older browsers might have had :)

In Javascript, function operators are bounded inside the containing function (or globally, if there is no function containing the function), they are not bounded inside the if or else block or loop. Thus, you cannot declare a function conditionally in this way (this can be done in another way, see below). And if you declare more than one function with the same name in the same scope, it will be overwritten first.

So what happens with your code:

  • Both function statements rise, but

  • Both of them have the same name, so the first is overwritten by the second.

  • Variable a has been changed, but not yet assigned a value.

  • The foo() statement is executed, writing to the log "b"

  • a to true .

  • If is executed. The condition is true, but neither one or the other branches actually do anything, because they do not contain operators other than function declarations that were raised earlier.

If you want to conditionally create functions, you need to declare a variable, and then assign a function expression to it. And then you cannot call the function until this assignment is:

 var foo; var a = true; if(a) foo = function() { console.log("a"); }; else foo = function() { console.log("b"); }; foo(); 
0
source share

In JavaScript, variables, function expressions, and function declarations rise at the top of the area.

A function declaration defines a named function variable that does not require variable assignment.

And it’s important to know that all function declaration text gets a popup.

eg.

 function outerFunction() { console.log(typeof functionDeclaration); // outputs "function" function functionDeclaration() { // ... function body } } 

This is due to the fact that due to the lifting, the code works as follows:

 function outerFunction() { function functionDeclaration() { // ... function body } console.log(typeof functionDeclaration); // outputs "function" } 

In your case, the declaration of the last function for foo rises at the top of the area, overriding all other function declarations. Therefore, it registers "b".

Variables and function expressions, however, can be raised without their assigned values.

eg.

 function outerFunction() { console.log(functionExpression); // outputs "undefined" var functionExpression = function () { // ... function body } } 

Performed more so

 function outerFunction() { var functionExpression = undefined; console.log(functionExpression); // outputs "undefined" functionExpression = function () { // ... function body } } 
0
source share

All Articles