Community

Start a Thread


Notifications
Subscribe You’re not receiving notifications from this thread.

Hoisting and Call Stack (Main Thread)

Here is the interview question prompt, presented for reference.

Understanding Variable Scoping in JavaScript

Let's dig into a fascinating JavaScript example to unravel the mystery of variable scoping.

The Code Snippet

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());

What Do You Expect?

Before running the code, what do you think will be printed to the console? Take a moment to think it over.

Execution Results

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 over 6 years ago by Jake from AlgoDaily

Jake from AlgoDaily Commented on Nov 30, 2017:

This is the main discussion thread generated for Hoisting and Call Stack.