Skip to content

Latest commit

 

History

History
33 lines (25 loc) · 533 Bytes

File metadata and controls

33 lines (25 loc) · 533 Bytes

What are the different scopes in JavaScript?

  • Global scope
  • Local scope

Global scope

var a = 1;
console.log(a); //1
console.log(window.a); //1

Local scope(function scope)

  • variable foo doesn't exist in a global scope. foo exists and be avaiable only inside local scope.
"use strict";

function moo(){
    var foo = 1;
}
console.log(foo);//foo is not defined 
  • JavaScript doesn't have the concept of block level scope.
for(var i=0; i< 3; i++){
    var j = 5;
}
console.log(j); //5