-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path8. Variable Scope.html
More file actions
86 lines (72 loc) · 2.21 KB
/
Copy path8. Variable Scope.html
File metadata and controls
86 lines (72 loc) · 2.21 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>JS Scope Visualizer</title>
<style>
body {
font-family: system-ui;
padding: 2rem;
color: #333;
}
.container {
max-width: 400px;
border: 2px solid #ddd;
padding: 20px;
border-radius: 12px;
}
button {
background: #f7df1e;
border: 1px solid #333;
padding: 10px 15px;
border-radius: 4px;
font-weight: bold;
cursor: pointer;
}
.scope-label {
font-size: 0.8rem;
color: #666;
text-transform: uppercase;
margin-bottom: 4px;
}
.result-box {
margin-top: 20px;
padding: 15px;
background: #f0f0f0;
border-left: 4px solid #f7df1e;
}
.error {
color: #d93025;
font-weight: bold;
}
</style>
</head>
<body>
<div class="container">
<h2>JS Scope Demo</h2>
<p>Global Variable: <code>"I am global"</code></p>
<button onclick="runScopeTest()">Run Scope Test</button>
<div id="output" class="result-box">
Click the button to see scope in action.
</div>
</div>
<script>
// GLOBAL SCOPE: Accessible from everywhere
let globalVar = "I am global";
function runScopeTest() {
// LOCAL (FUNCTION) SCOPE: Only accessible inside these curly braces
let localVar = "I am local";
let output = document.getElementById("output");
output.innerHTML = `
<div class="scope-label">Inside the Function:</div>
✅ Global: ${globalVar}<br>
✅ Local: ${localVar}<br>
<hr>
<div class="scope-label">Outside the Function:</div>
✅ Global: ${globalVar}<br>
❌ Local: <span class="error">ReferenceError</span>
`;
}
</script>
</body>
</html>