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
+14-12
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,11 +205,11 @@ 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
-
###IIFE
210
+
## IIFE
209
211
210
-
As in the past there was only `var`, and it has no block-level visibility, programmers invented a way to emulate it. What they did was called "immediately-invoked function expressions" (abbreviated as IIFE).
212
+
In the past, as there was only `var`, and it has no block-level visibility, programmers invented a way to emulate it. What they did was called "immediately-invoked function expressions" (abbreviated as IIFE).
211
213
212
214
That's not something we should use nowadays, but you can find them in old scripts.
213
215
@@ -216,22 +218,22 @@ An IIFE looks like this:
216
218
```js run
217
219
(function() {
218
220
219
-
let message ="Hello";
221
+
var message ="Hello";
220
222
221
223
alert(message); // Hello
222
224
223
225
})();
224
226
```
225
227
226
-
Here a Function Expression is created and immediately called. So the code executes right away and has its own private variables.
228
+
Here, a Function Expression is created and immediately called. So the code executes right away and has its own private variables.
227
229
228
-
The Function Expression is wrapped with parenthesis `(function {...})`, because when JavaScript meets `"function"` in the main code flow, it understands it as the start of a Function Declaration. But a Function Declaration must have a name, so this kind of code will give an error:
230
+
The Function Expression is wrapped with parenthesis `(function {...})`, because when JavaScript engine encounters `"function"` in the main code, it understands it as the start of a Function Declaration. But a Function Declaration must have a name, so this kind of code will give an error:
229
231
230
232
```js run
231
-
//Try to declare and immediately call a function
233
+
//Tries to declare and immediately call a function
232
234
function() { // <-- Error: Function statements require a function name
233
235
234
-
let message ="Hello";
236
+
var message ="Hello";
235
237
236
238
alert(message); // Hello
237
239
@@ -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