namaste-javascript-notes

Episode 4 : Functions and Variable Environments

var x = 1;
a();
b(); // we are calling the functions before defining them. This will work properly, as seen in Hoisting.
console.log(x); // 3

function a() {
  var x = 10; // localscope because of separate execution context
  console.log(x); // 1
}

function b() {
  var x = 100;
  console.log(x); // 2
}

Outputs:

10

100

1

Code Flow in terms of Execution Context

Call Stack : GEC

Call Stack: [GEC, a()]

Call Stack: GEC

Call Stack :[GEC, b()] -> GEC (after printing yet another totally different x value as 100 in console log)

Execution Context Phase 1


Watch Live On Youtube below:

Functions and Variable Environments Youtube Link