forked from aayushyadavz/JavaScript-Full-Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Learned about Nested Scope & some interesting concepts related to functions executions.
- Loading branch information
1 parent
316c5e9
commit 568bf77
Showing
1 changed file
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// 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 ***********************************/ | ||
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 |