0
function test(flag){
    if (flag) {
        return function test1(){console.log(1)}
    }else{
        return function test1(){console.log(2)}
    }
}
test(true)()
test()()

it log 1 and 2,why not double 2? how does this works

my english is not very good, thank you

this also works with 1 and 2

function test(flag){
    if (flag) {
        function test1(){console.log(1)}
        return test1
    }else{
        function test1(){console.log(2)}
        return test1
    }
}
test(true)()
test()()
3
  • 1
    Thats because in the first call flag is passed as true and hence the true block is executed and in the second call flag will be passed as undefined and so the false block will get executed Commented Jun 22, 2016 at 3:53
  • On the first call, flag is true, then executes the if condition. On the second, flag is null, which is false, then executes the else condition. Commented Jun 22, 2016 at 4:05
  • 2
    Don't write code like this. Commented Jun 22, 2016 at 4:07

2 Answers 2

1

The function in this line:

return function test1(){console.log(2)}

Is not a function declaration. It is a named function expression, because it is part of a statement.

Function expressions are not hoisted. Only function declarations are hoisted, like this:

function test(){
    return test1;

    function test1() { console.log(1); }
    function test1() { console.log(2); }
}

test()();

Edit: Regarding the question you added after the fact, function declarations inside conditional expressions have undefined behavior and you can see different results depending on your JavaScript engine. Functions inside if-else statements may not be hoisted to the top of the scope, and you should not put function declarations inside conditional expressions. More about this

Sign up to request clarification or add additional context in comments.

3 Comments

function test(flag){ if (flag) { function test1(){console.log(1)} return test1 }else{ function test1(){console.log(2)} return test1 } } test(true)() test()() this also 1 and 2
function test(flag){ if (flag) { function test1(){console.log(1)} return test1 }else{ function test1(){console.log(2)} return test1 } } test(true)() test()()
@user1861806 Function declarations inside conditional expressions have undefined behavior and you can see different results depending on your JavaScript engine. Hoisting is not guaranteed inside an if-else expression and you should not put functions inside conditional expressions.
0

In the first call test(true)() it goes through:

if (flag) {
        return function test1(){console.log(1)}
    } 

because the flag has value true.

In the second call test()() it goes through the else path:

else{
        return function test1(){console.log(2)}
    }

because in that instance value of flag is undefined and it evaluates as false.

You can get an idea about truthy and falsy using these links.

Hope you got the idea here. Please let me know if you have any questions.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.