Skip to content

Commit 99caa80

Browse files
committed
Fix #2098 - replace let with var in IIFE example
1 parent 9f686c9 commit 99caa80

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

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

+14-12
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,11 +205,11 @@ 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

208-
### IIFE
210+
## IIFE
209211

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).
211213

212214
That's not something we should use nowadays, but you can find them in old scripts.
213215

@@ -216,22 +218,22 @@ An IIFE looks like this:
216218
```js run
217219
(function() {
218220

219-
let message = "Hello";
221+
var message = "Hello";
220222

221223
alert(message); // Hello
222224

223225
})();
224226
```
225227

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

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:
229231

230232
```js run
231-
// Try to declare and immediately call a function
233+
// Tries to declare and immediately call a function
232234
function() { // <-- Error: Function statements require a function name
233235

234-
let message = "Hello";
236+
var message = "Hello";
235237

236238
alert(message); // Hello
237239

@@ -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)