- What is the scope of variable GLOBAL_DATA in the example below:
Pretend in the console, that we type: console.log(GLOBAL_DATA);
<script>
let GLOBAL_DATA = { value : 1};
</script>
- What is the scope of variable x in the examples below:
let x = 1; // global
{
let x = 2; //local
}
console.log(x); //1
var x = 1; // global
{
var x = 2; // global
}
console.log(x); //2
- What is the scope of variable x in the examples below:
function outerFn(){
let x = 1;
function log(){
console.log(x);
};
function run(fn){
let x = 100;
fn();
}
run(log);
};
outerFn(); //1
- What do we call this method of scoping:
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
- What is the difference between global scope and other scope types and when do we use the global scope?
- What is the difference between let and var defining a variable?
- What is the difference between strict mode and sloppy mode?
Hande Nur Demirbay, Joud Khanji, Rayan Alrouh
1- it will print the object { value : 1}
2-
function outerFn(){
let x = 1; // local
function log(){
console.log(x);
};
function run(fn){
let x = 100; // local
fn();
}
run(log);
};
outerFn(); //1
3-
function outerFn(){
let x = 1; // local
function log(){
console.log(x);
};
function run(fn){
let x = 100; // local
fn();
}
run(log);
};
outerFn(); //1
4- Scope Chain
5- Global can declare outside of a block. Local for variables declared inside of a block.
and we use the global scope for variables we will use it inside many functions
6- just don't use var ;)
7- Strict mode is a way to enforce stricter rules on JavaScript code.
-In strict mode, assigning a value to an undeclared variable will result in an error.
-In strict mode, using a function name as a variable name will result in an error.
-In strict mode, duplicate property names in objects will result in an error.