Skip to content Skip to sidebar Skip to footer

Are Functions Set Before Variables In The Javascript 'creation Phase'?

I am doing the Udemy course Javascript: Understanding the Weird Parts right now, and I just learned about the creation phase and the execution phase that occurs when the interprete

Solution 1:

You can think of it like this.
Your original code:

b();

function b () {

  console.log(a);
}

var a = 'peas';

b();

is actually executed like this:

var a;
function b () {
  console.log(a);
}

b(); // log undefined because a doesn't have a value yet

a = 'peas';

b(); // log peas because a has a value

Basically all the variable and function definitions are hoisted at the top of the enclosing scope. The order doesn't really matter because the code inside the b function doesn't get executed until you actually call the function.


Post a Comment for "Are Functions Set Before Variables In The Javascript 'creation Phase'?"