var counter = 0;
function add(){
var counter = 0;
counter+=1;
}
add();
add();
add();
console.log(counter); //output: 0
also how the GEC (Global execution context) and FEC (Functional execution context) will be created?
>Solution :
Lets take a closer look:
-
The add function has an internally defined
countervariable. So it does not change the globally definedcountervariable. -
At the first line you have
var counter = 0;. According to (1) there is no code to change the value of the globally definedcounter. So at the last line the value would be 0.