Search code examples
javascriptfunctionvariableslet

Which function is executed if both has same variable names n declared?


let foo = function(){
    console.log(1);
}
setTimeout (foo,1000);

foo = function(){
    console.log(2);
}  

I got the output as 1. But I need to know why because let can be reinitialized so its correct over here why didn't I get the o/p as 2?


Solution

  • The second variable is not declared when run this line setTimeout (foo,1000); you can check it by commenting the let foo = function(){ console.log(1); }