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: 1-js/06-advanced-functions/04-var/article.md
+7-5Lines changed: 7 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -28,7 +28,7 @@ On the other hand, it's important to understand differences when migrating old s
28
28
29
29
## "var" has no block scope
30
30
31
-
Variables, declared with `var`, are either function-wide or global. They are visible through blocks.
31
+
Variables, declared with `var`, are either function-scoped or global-scoped. They are visible through blocks.
32
32
33
33
For instance:
34
34
@@ -60,11 +60,13 @@ The same thing for loops: `var` cannot be block- or loop-local:
60
60
61
61
```js
62
62
for (var i =0; i <10; i++) {
63
+
var one =1;
63
64
// ...
64
65
}
65
66
66
67
*!*
67
-
alert(i); // 10, "i" is visible after loop, it's a global variable
68
+
alert(i); // 10, "i" is visible after loop, it's a global variable
69
+
alert(one); // 1, "one" is visible after loop, it's a global variable
68
70
*/!*
69
71
```
70
72
@@ -83,7 +85,7 @@ sayHi();
83
85
alert(phrase); // Error: phrase is not defined (Check the Developer Console)
84
86
```
85
87
86
-
As we can see, `var` pierces through `if`, `for` or other code blocks. That's because a long time ago in JavaScript blocks had no Lexical Environments. And `var` is a remnant of that.
88
+
As we can see, `var` pierces through `if`, `for` or other code blocks. That's because a long time ago in JavaScript, blocks had no Lexical Environments. And `var` is a remnant of that.
87
89
88
90
## "var" tolerates redeclarations
89
91
@@ -203,7 +205,7 @@ sayHi();
203
205
204
206
Because all `var` declarations are processed at the function start, we can reference them at any place. But variables are undefined until the assignments.
205
207
206
-
In both examples above `alert` runs without an error, because the variable `phrase` exists. But its value is not yet assigned, so it shows `undefined`.
208
+
In both examples above,`alert` runs without an error, because the variable `phrase` exists. But its value is not yet assigned, so it shows `undefined`.
207
209
208
210
## IIFE
209
211
@@ -277,7 +279,7 @@ In all the above cases we declare a Function Expression and run it immediately.
277
279
278
280
There are two main differences of `var` compared to `let/const`:
279
281
280
-
1.`var` variables have no block scope, they are visible minimum at the function level.
282
+
1.`var` variables have no block scope, they are visibility is scoped to current function, or global, if declared outside function.
281
283
2.`var` declarations are processed at function start (script start for globals).
282
284
283
285
There's one more very minor difference related to the global object, that we'll cover in the next chapter.
0 commit comments