-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path04_scope.js
49 lines (39 loc) · 1.25 KB
/
04_scope.js
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
49
// 4. Nested Scope,
function one () {
const username = "Ayush"
function two () {
const website = "Youtube"
console.log(username);
// Note: function two can access the variables of function one
}
// console.log(website); // Output: Error
// Note: We cannot access the website variable outside function two
two()
}
one()
/* Output: Ayush
Note: In nested function child can access parent variables.
--------------------------------------------------------------------------*/
if (true) {
const username = "Ayush"
if (username === "Ayush") /* (true) */ {
const website = " Youtube"
console.log(username + website);
// We can access username variable in child if statement.
}
// console.log(website); // Output: Error
// Note: We cannot access website variable outside the scope.
}
// console.log(username); // Output: Error
// Note: We cannot access username variable outside the scope.
/* Output: Ayush Youtube
**************************** Interesting **********************************
Hoisting, */
console.log(addone(5));
function addone (num) {
return num + 1
} // Output: 6
// console.log(addtwo(5));
const addtwo = function (num){
return num + 2
} // Output: error