<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
shad mustafa-fadi moayad-rokaya amjaad- mhamad marshall
1-Global scope
2- let-(block scope), var-(global scope)
3-function scope
4- lexical scope method
5-global scope : can define anywhere in js , functional scope: can define only inside of function , block scope: can define only inside of block
6-let can't be redeclared and it's a block , functional and global scope, but var can be declared and it is global scope
7-JavaScript by default is in sloppy mode so by adding strict mode above the js codes it It removes different JavaScript silent errors by changing them to throw errors. It repairs mistakes that mark it difficult for JavaScript engines to do optimizations. and also It makes it relaxed to write secure JavaScript.