Skip to content

Commit 6683781

Browse files
committed
Change some sentences to make it more accurate - #2098
1 parent 76f3ee9 commit 6683781

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

1-js/06-advanced-functions/04-var/article.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ On the other hand, it's important to understand differences when migrating old s
2828

2929
## "var" has no block scope
3030

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.
3232

3333
For instance:
3434

@@ -60,11 +60,13 @@ The same thing for loops: `var` cannot be block- or loop-local:
6060

6161
```js
6262
for (var i = 0; i < 10; i++) {
63+
var one = 1;
6364
// ...
6465
}
6566

6667
*!*
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
6870
*/!*
6971
```
7072

@@ -83,7 +85,7 @@ sayHi();
8385
alert(phrase); // Error: phrase is not defined (Check the Developer Console)
8486
```
8587

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.
8789

8890
## "var" tolerates redeclarations
8991

@@ -203,7 +205,7 @@ sayHi();
203205

204206
Because all `var` declarations are processed at the function start, we can reference them at any place. But variables are undefined until the assignments.
205207

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`.
207209

208210
## IIFE
209211

@@ -277,7 +279,7 @@ In all the above cases we declare a Function Expression and run it immediately.
277279

278280
There are two main differences of `var` compared to `let/const`:
279281

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.
281283
2. `var` declarations are processed at function start (script start for globals).
282284

283285
There's one more very minor difference related to the global object, that we'll cover in the next chapter.

0 commit comments

Comments
 (0)