Skip to content

Commit 0ad5a13

Browse files
committed
scope lab
1 parent 2bf0157 commit 0ad5a13

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
## Summary
2+
3+
1. Variable scope overview:
4+
5+
Global scope: Variables declared outside any block or function have a global scope and are accessible throughout the entire script.
6+
7+
Block scope: Variables declared within curly braces {} have block scope, accessible only within that block.
8+
9+
Function scope: Variables declared within a function have function scope, limited to that function's block.
10+
11+
2. Variable declaration and initialization:
12+
13+
Used var, let, and const to declare and initialize variables in different scopes: global, block, and function.
14+
15+
Demonstrated the behavior of these variables in respective scopes by accessing them outside their defined scope.
16+
17+
3. Output and scope analysis:
18+
19+
Global variables were accessible everywhere in the script.
20+
21+
Block-scoped variables (inside {}) had limited accessibility, resulting in ReferenceErrors when accessed outside their blocks.
22+
23+
Function-scoped variables (inside a function) also led to ReferenceErrors when accessed outside the function.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<title>Scoped Variables</title>
5+
</head>
6+
<body>
7+
<h1>Scope Lab for Var, Let and Const</h1>
8+
</h1>
9+
<script src="./scope_lab.js"></script>
10+
</body>
11+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Global scope
2+
var globalVar = "I'm a global variable";
3+
let globalLet = "I'm also global, but scoped with let";
4+
const globalConst = "I'm a global constant";
5+
6+
{
7+
// Block scope
8+
var blockVar = "I'm a block-scoped var";
9+
let blockLet = "I'm a block-scoped let";
10+
const blockConst = "I'm a block-scoped const";
11+
}
12+
13+
// Global scope
14+
console.log(globalVar);
15+
console.log(globalLet);
16+
console.log(globalConst);
17+
18+
// Block Scope
19+
20+
// You will see output for blockVar, because it can be accessed outside the curly brackets {}
21+
console.log(blockVar);
22+
23+
/* But you will see ReferenceError for blockLet because it cannot be accessed outside the curly brackets due to which it is known as block scoped variables. Such variables can only be accessed within curly brackets.
24+
You will see the same error for const as well. */
25+
// console.log(blockLet);
26+
// console.log(blockConst);
27+
28+
// In this step, you will explore how function-scoped variables work.
29+
30+
function show() {
31+
var functionVar = "I'm a block-scoped var";
32+
let functionLet = "I'm a block-scoped let";
33+
const functionConst = "I'm a block-scoped const";
34+
}
35+
show();
36+
37+
/* You will see ReferenceError for functionVar because it cannot be accessed outside the function making it a function scoped variable */
38+
console.log(functionVar); // Throws ReferenceError
39+
console.log(functionLet); // Throws ReferenceError
40+
console.log(functionConst); // Throws ReferenceError

0 commit comments

Comments
 (0)