<script>
let GLOBAL_DATA = {value : 1};
</script>
Pretend in the console, that we type:
console.log(GLOBAL_DATA);
let x = 1; // global
{
let x = 2; //local
}
console.log(x); //1
var x = 1; // global
{
var x = 2; // global
}
console.log(x); //2
function outerFn() {
let x = 1;
function log() {
console.log(x);
};
function run(fn) {
let x = 100;
fn();
}
run(log);
};
outerFn(); //1
let x0 = 0;
function fn1() {
let x1 = 1;
fn2();
function fn2() {
let x2 = 2;
fn3();
function fn3() {
let x3 = 3;
console.log(x0 + " " + x1 + " " + x2 + " " + x3);
};
};
};
fn1();//0 1 2 3
Team: Hala, Ali Majed, Aland, Wafaa, Maryam
1- Global scope
2- outside { } is global - inside { } with let is local - inside { } with var is global
3- function scope
4- chain scoping
5- global scope can be used either inside a function or outside of it while others have restricted use/ we use global scopes when want the variables to be public
6- let is block scope (limited) and var is global scope
7- when we use var in strict mode it won't make it global and sloppy mode is the default mode (not strict)