-
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 5 types of Scopes: Global, function, block, module and …
…lexical
- Loading branch information
Showing
2 changed files
with
51 additions
and
0 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
03 - Chai aur Javascript/16 - Lexical Scoping and Closures/01_scopes.js
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,48 @@ | ||
// 1. Global Scope | ||
|
||
let globalVar = 'I am Global' | ||
|
||
function checkGlobalScope(){ | ||
console.log(globalVar); // Accessible here | ||
} | ||
|
||
checkGlobalScope() | ||
console.log(globalVar); // Accessible here too | ||
|
||
|
||
// 2. Function Scope | ||
|
||
function checkFunctionScope(){ | ||
let functionVar = "I am Function Variable"; | ||
console.log(functionVar); // Accessible here | ||
} | ||
|
||
checkFunctionScope(); | ||
// console.log(functionVar) // Error: functionVar is not defined | ||
|
||
|
||
// 3. Block Scope | ||
|
||
if(true){ | ||
let blockVar = 'I am Blocked Variable' | ||
console.log(blockVar); // Accesible here | ||
} | ||
|
||
// console.log(blockVar); // Error: blockVar is not defined | ||
|
||
// 4. Module Scope | ||
// const myModule = require('./myModule.js') | ||
// console.log(myModule); // Works because it's imported | ||
|
||
// Lexical Scope | ||
|
||
function outer(){ | ||
const outerVar = "I am Outer" | ||
|
||
function inner(){ | ||
console.log(outerVar); // Accesible here due to lexical scoping | ||
} | ||
inner() | ||
} | ||
|
||
outer() |
3 changes: 3 additions & 0 deletions
3
03 - Chai aur Javascript/16 - Lexical Scoping and Closures/myModule.js
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,3 @@ | ||
const moduleVar = "I am Module-Scoped" | ||
|
||
export default moduleVar; |