-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathscoping.js
More file actions
48 lines (41 loc) · 1.32 KB
/
scoping.js
File metadata and controls
48 lines (41 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
* let is block scoped that means any let statment in {} cannot be accessed outside
*
* var is a function scoped that means any var statement in function ex(){} cannot be used outside the funtion
*
* const is same as let but its value cannot be changed.
*/
//--------------------- let ---------------------------
//====== NOT WORKING CODE =========
//any conditional or loop statement before {}
{
let variable = 10;
}
// This will trow the error because it is inside a block
// console.log(variable);
//====== WORKING CODE =============
//any conditional or loop statement before {}
let variable1;
{
variable1 = 10;
}
//executed successfully because let is outside of the {} block
// console.log(variable1);
//--------------------- var ---------------------------
//====== NOT WORKING CODE =========
function dream(){
var iFirstWant = 'Burger';
}
// This will trow the error as iFirstWant is inside function
console.log(iFirstWant);
//========= WORKING CODE ============
//any conditional or loop statement before {}
{
var iWant = 'Pizza';
}
//executed successfully because let is outside of the {} block
console.log(iWant);
//--------------------- const ---------------------------
//Now you cannot reassign the value to the myName if you try then it will throw error
const myName = 'Kaiwalya';
console.log(myName);