Here is the interview question prompt, presented for reference.
Let's dig into a fascinating JavaScript example to unravel the mystery of variable scoping.
Here's the code we'll be examining:
var numOfBeers = 5;
function getMoreBeers() {
console.log('I have this many beers: ' + numOfBeers);
var numOfBeers = 25;
return numOfBeers;
}
console.log('I now have this many beers: ' + getMoreBeers());
Before running the code, what do you think will be printed to the console? Take a moment to think it over.
Go ahead and execute the code. What do you see?
Your job in the interview is to explain why it runs the way it does.
You can see the full challenge with visuals at this link.
Challenges • Asked almost 7 years ago by Jake from AlgoDaily
This is the main discussion thread generated for Hoisting and Call Stack.