scopes javascript code example

Example 1: scope.js

//let is block scoped
//const is block scoped


//var => function scoped

var a = 20;

console.log(a);

if(true){
      var a = 50;
      console.log(a);
}

function callMe(){
      var a = 100;
      console.log("Inside call me");
      console.log(a);
}
callMe();

console.log(a);

Example 2: javascript define global variable

window.myGlobalVariable = "I am totally a global Var"; //define a global variable
var myOtherGlobalVariable="I too am global as long as I'm outside a function";

Example 3: global scope js

const color = 'blue'

const returnSkyColor = () => {
  return color; // blue 
};

console.log(returnSkyColor()); // blue

Tags:

Misc Example