You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/javascript/all-about-variables/variable-scopes.md
+16-17Lines changed: 16 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,7 +13,6 @@ Understanding variable scope in JavaScript is essential for writing effective an
13
13
14
14
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.
15
15
16
-
17
16
## What Is Scope?
18
17
19
18
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:
42
41
let globalVar ="I am a global variable";
43
42
44
43
functionaccessGlobalVar() {
45
-
console.log(globalVar); // Accessible here
44
+
console.log(globalVar); // Accessible here
46
45
}
47
46
48
47
accessGlobalVar(); // Outputs: I am a global variable
@@ -62,8 +61,8 @@ Here's an example of a variable declared in a local scope:
62
61
63
62
```javascript title="app.js" showLineNumbers
64
63
functionmyFunction() {
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
67
66
}
68
67
69
68
myFunction(); // Outputs: I am a local variable
@@ -83,8 +82,8 @@ Here's an example of a variable declared in a block scope:
83
82
84
83
```javascript title="app.js" showLineNumbers
85
84
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
88
87
}
89
88
90
89
console.log(blockVar); // Error: blockVar is not defined
@@ -104,8 +103,8 @@ Here's an example of variable shadowing:
104
103
let name ="Global Name";
105
104
106
105
functiondisplayName() {
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
109
108
}
110
109
111
110
displayName();
@@ -125,11 +124,11 @@ Here's an example illustrating function scope with `var`:
125
124
126
125
```javascript title="app.js" showLineNumbers
127
126
functionmyFunction() {
128
-
if (true) {
129
-
var varVariable ="I am a var variable";
130
-
}
127
+
if (true) {
128
+
var varVariable ="I am a var variable";
129
+
}
131
130
132
-
console.log(varVariable); // Accessible here
131
+
console.log(varVariable); // Accessible here
133
132
}
134
133
135
134
myFunction(); // Outputs: I am a var variable
@@ -149,11 +148,11 @@ Here's an example illustrating block scope with `let`:
149
148
150
149
```javascript title="app.js" showLineNumbers
151
150
if (true) {
152
-
let letVariable ="I am a let variable";
153
-
constconstVariable="I am a const variable";
151
+
let letVariable ="I am a let variable";
152
+
constconstVariable="I am a const variable";
154
153
155
-
console.log(letVariable); // Accessible here
156
-
console.log(constVariable); // Accessible here
154
+
console.log(letVariable); // Accessible here
155
+
console.log(constVariable); // Accessible here
157
156
}
158
157
159
158
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
166
165
167
166
## Conclusion
168
167
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