What is the role var statement witin same functions, wich leads to different results ?

function foo(a, b=()=> a) { a = 1; return b(); } console.log(foo(2));//1 //******** function bar(a, b=()=> a) { var a = 1; return b(); } console.log(bar(2)); //2

9/14/2019 8:21:51 PM

Volodya Panosyan

1 Answer

New Answer


In function foo, a is just being used as an identifier or a call back if you will. In function bar, you are stating that var a has the value of 1 from inside the scope of the function which wouldn't be called until you return b() that has a as an expression. For bar(a) "the first parameter" a is being used when you call bar(2) is where the a is being used as identifier/callback. I'm bad at explaining things lol. I don't know if this will help you or hurt you lol.