Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why the answer of counter = 0?

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Lets take a closer look:

  1. The add function has an internally defined counter variable. So it does not change the globally defined counter variable.

  2. At the first line you have var counter = 0;. According to (1) there is no code to change the value of the globally defined counter. So at the last line the value would be 0.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading