commit
f91d41406e
1 changed files with 4 additions and 4 deletions
|
@ -52,7 +52,7 @@ If we used `let test` on the 2nd line, then it wouldn't be visible to `alert`. B
|
|||
The same thing for loops: `var` cannot be block- or loop-local:
|
||||
|
||||
```js
|
||||
for(var i = 0; i < 10; i++) {
|
||||
for (var i = 0; i < 10; i++) {
|
||||
// ...
|
||||
}
|
||||
|
||||
|
@ -61,7 +61,7 @@ alert(i); // 10, "i" is visible after loop, it's a global variable
|
|||
*/!*
|
||||
```
|
||||
|
||||
If a code block in inside a function, then `var` becomes a function-level variable:
|
||||
If a code block is inside a function, then `var` becomes a function-level variable:
|
||||
|
||||
```js
|
||||
function sayHi() {
|
||||
|
@ -76,7 +76,7 @@ sayHi();
|
|||
alert(phrase); // Error: phrase is not defined
|
||||
```
|
||||
|
||||
As we can see, `var` pierces through `if`, `for` or other code blocks. That's because long time ago in JavaScript blocks had no Lexical Environments. And `var` is a reminiscence of that.
|
||||
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 reminiscence of that.
|
||||
|
||||
## "var" are processed at the function start
|
||||
|
||||
|
@ -184,4 +184,4 @@ There are two main differences of `var`:
|
|||
|
||||
There's one more minor difference related to the global object, we'll cover that in the next chapter.
|
||||
|
||||
These differences are actually a bad thing most of time. First, we can't create block-local variables. And hoisting just creates more space for errors. So, for new scripts `var` is used exceptionally rarely.
|
||||
These differences are actually a bad thing most of the time. First, we can't create block-local variables. And hoisting just creates more space for errors. So, for new scripts `var` is used exceptionally rarely.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue