File tree Expand file tree Collapse file tree 1 file changed +47
-0
lines changed Expand file tree Collapse file tree 1 file changed +47
-0
lines changed Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments