-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScope.js
More file actions
48 lines (39 loc) · 1.92 KB
/
Copy pathScope.js
File metadata and controls
48 lines (39 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// 🌍 Global Scope
var globalVar = "I am global var";
let globalLet = "I am global let";
const globalConst = "I am global const";
console.log("🌍 Global Scope:");
console.log(globalVar); // ✅ accessible
console.log(globalLet); // ✅ accessible
console.log(globalConst); // ✅ accessible
function testFunctionScope() {
// 🔧 Function Scope
var functionVar = "I am function var";
let functionLet = "I am function let";
const functionConst = "I am function const";
console.log("\n🔧 Function Scope:");
console.log(functionVar); // ✅ accessible
console.log(functionLet); // ✅ accessible
console.log(functionConst); // ✅ accessible
if (true) {
// 🧱 Block Scope inside function
directVariable = 'I am a direct variable (inside function)';
var blockVar = "I am block var (inside function)";
let blockLet = "I am block let (inside function)";
const blockConst = "I am block const (inside function)";
console.log("\n🧱 Block Scope (Inside if block):");
console.log(blockVar); // ✅ accessible
console.log(blockLet); // ✅ accessible
console.log(blockConst); // ✅ accessible
}
console.log("\n🔍 After Block Inside Function:"); //__________let and const having block level scope________
console.log(blockVar); // ✅ var leaks out of block
// console.log(blockLet); // ❌ ReferenceError
// console.log(blockConst); // ❌ ReferenceError
}
testFunctionScope();
console.log("\n🚫 Outside Function:"); //_______Var / let / const having function scope______
console.log(directVariable); //_______direct variable having a Global scope______
// console.log(functionVar); // ❌ ReferenceError
// console.log(functionLet); // ❌ ReferenceError
// console.log(functionConst); // ❌ ReferenceError