Skip to content

Commit 7e55e32

Browse files
committed
Merge branch 'dev-3' of https://github.com/CodeHarborHub/codeharborhub into dev-3
2 parents 6bcf574 + e058907 commit 7e55e32

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

docs/javascript/all-about-variables/variable-scopes.md

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ Understanding variable scope in JavaScript is essential for writing effective an
1313

1414
In this tutorial, we'll explore the different types of scopes in JavaScript, how they work, and how you can use them to control the accessibility of your variables.
1515

16-
1716
## What Is Scope?
1817

1918
In JavaScript, scope refers to the current context of code execution. It determines which variables and functions are accessible at a given point in your code. Think of scope as the set of rules that govern where and how variables and functions can be accessed or referenced.
@@ -42,7 +41,7 @@ Here's an example of a variable declared in the global scope:
4241
let globalVar = "I am a global variable";
4342

4443
function accessGlobalVar() {
45-
console.log(globalVar); // Accessible here
44+
console.log(globalVar); // Accessible here
4645
}
4746

4847
accessGlobalVar(); // Outputs: I am a global variable
@@ -62,8 +61,8 @@ Here's an example of a variable declared in a local scope:
6261

6362
```javascript title="app.js" showLineNumbers
6463
function myFunction() {
65-
let localVar = "I am a local variable";
66-
console.log(localVar); // Accessible here
64+
let localVar = "I am a local variable";
65+
console.log(localVar); // Accessible here
6766
}
6867

6968
myFunction(); // Outputs: I am a local variable
@@ -83,8 +82,8 @@ Here's an example of a variable declared in a block scope:
8382

8483
```javascript title="app.js" showLineNumbers
8584
if (true) {
86-
let blockVar = "I am a block-scoped variable";
87-
console.log(blockVar); // Accessible here
85+
let blockVar = "I am a block-scoped variable";
86+
console.log(blockVar); // Accessible here
8887
}
8988

9089
console.log(blockVar); // Error: blockVar is not defined
@@ -104,8 +103,8 @@ Here's an example of variable shadowing:
104103
let name = "Global Name";
105104

106105
function displayName() {
107-
let name = "Local Name"; // Shadows the global variable
108-
console.log(name); // Outputs: Local Name
106+
let name = "Local Name"; // Shadows the global variable
107+
console.log(name); // Outputs: Local Name
109108
}
110109

111110
displayName();
@@ -125,11 +124,11 @@ Here's an example illustrating function scope with `var`:
125124

126125
```javascript title="app.js" showLineNumbers
127126
function myFunction() {
128-
if (true) {
129-
var varVariable = "I am a var variable";
130-
}
127+
if (true) {
128+
var varVariable = "I am a var variable";
129+
}
131130

132-
console.log(varVariable); // Accessible here
131+
console.log(varVariable); // Accessible here
133132
}
134133

135134
myFunction(); // Outputs: I am a var variable
@@ -149,11 +148,11 @@ Here's an example illustrating block scope with `let`:
149148

150149
```javascript title="app.js" showLineNumbers
151150
if (true) {
152-
let letVariable = "I am a let variable";
153-
const constVariable = "I am a const variable";
151+
let letVariable = "I am a let variable";
152+
const constVariable = "I am a const variable";
154153

155-
console.log(letVariable); // Accessible here
156-
console.log(constVariable); // Accessible here
154+
console.log(letVariable); // Accessible here
155+
console.log(constVariable); // Accessible here
157156
}
158157

159158
console.log(letVariable); // Error: letVariable is not defined
@@ -166,4 +165,4 @@ Using `let` and `const` for variable declarations helps you write more robust an
166165

167166
## Conclusion
168167

169-
Understanding variable scopes in JavaScript is crucial for writing clean, maintainable code. By knowing how global, local, and block scopes work, you can control the visibility and accessibility of variables in your code, prevent naming conflicts, and reduce the chances of bugs.
168+
Understanding variable scopes in JavaScript is crucial for writing clean, maintainable code. By knowing how global, local, and block scopes work, you can control the visibility and accessibility of variables in your code, prevent naming conflicts, and reduce the chances of bugs.

0 commit comments

Comments
 (0)