Skip to content

Commit 568bf77

Browse files
committed
04 Scope JS
Learned about Nested Scope & some interesting concepts related to functions executions.
1 parent 316c5e9 commit 568bf77

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

03_Basics/04_scope.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// 4. Nested Scope
2+
3+
function one () {
4+
const username = "Ayush"
5+
6+
function two () {
7+
const website = "Youtube"
8+
console.log(username);
9+
// Note: function two can access the variables of function one
10+
}
11+
// console.log(website); // Output: Error
12+
// Note: We cannot access the website variable outside function two
13+
14+
two()
15+
}
16+
one()
17+
/* Output: Ayush
18+
Note: In nested function child can access parent variables.
19+
20+
--------------------------------------------------------------------------*/
21+
if (true) {
22+
const username = "Ayush"
23+
if (username === "Ayush") /* (true) */ {
24+
const website = " Youtube"
25+
console.log(username + website);
26+
// We can access username variable in child if statement.
27+
}
28+
// console.log(website); // Output: Error
29+
// Note: We cannot access website variable outside the scope.
30+
}
31+
// console.log(username); // Output: Error
32+
// Note: We cannot access username variable outside the scope.
33+
/* Output: Ayush Youtube
34+
35+
**************************** Interesting ***********************************/
36+
console.log(addone(5));
37+
38+
function addone (num) {
39+
return num + 1
40+
} // Output: 6
41+
42+
43+
// console.log(addtwo(5));
44+
45+
const addtwo = function (num){
46+
return num + 2
47+
} // Output: error

0 commit comments

Comments
 (0)