Skip to content

Commit

Permalink
04 Scope JS
Browse files Browse the repository at this point in the history
Learned about Nested Scope & some interesting concepts related to functions executions.
  • Loading branch information
aayushyadavz committed May 10, 2024
1 parent 316c5e9 commit 568bf77
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions 03_Basics/04_scope.js
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

0 comments on commit 568bf77

Please sign in to comment.