Skip to content

Commit 46a9bb5

Browse files
committed
use strict for FD visibility
1 parent 27a6d60 commit 46a9bb5

File tree

1 file changed

+13
-14
lines changed
  • 1-js/02-first-steps/15-function-expressions-arrows

1 file changed

+13
-14
lines changed

1-js/02-first-steps/15-function-expressions-arrows/article.md

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -196,19 +196,20 @@ First, the syntax: how to differentiate between them in the code.
196196

197197
The more subtle difference is *when* a function is created by the JavaScript engine.
198198

199-
**A Function Expression is created when the execution reaches it and is usable from then on.**
199+
**A Function Expression is created when the execution reaches it and is usable only from that moment.**
200200

201201
Once the execution flow passes to the right side of the assignment `let sum = function…` -- here we go, the function is created and can be used (assigned, called, etc. ) from now on.
202202

203203
Function Declarations are different.
204204

205-
**A Function Declaration is usable in the whole script (or a code block, if it's inside a block).**
205+
**A Function Declaration can be called earlier than it is defined.**
206206

207-
In other words, when JavaScript *prepares* to run the script or a code block, it first looks for Function Declarations in it and creates the functions. We can think of it as an "initialization stage".
207+
For example, a global Function Declaration is visible in the whole script, no matter where it is.
208208

209-
And after all of the Function Declarations are processed, the execution goes on.
209+
That's due to internal algorithms. When JavaScript prepares to run the script, it first looks for global Function Declarations in it and creates the functions. We can think of it as an "initialization stage".
210+
211+
And after all Function Declarations are processed, the code is executed. So it has access to these functions.
210212

211-
As a result, a function declared as a Function Declaration can be called earlier than it is defined.
212213

213214
For example, this works:
214215

@@ -224,7 +225,7 @@ function sayHi(name) {
224225

225226
The Function Declaration `sayHi` is created when JavaScript is preparing to start the script and is visible everywhere in it.
226227

227-
...If it was a Function Expression, then it wouldn't work:
228+
...If it were a Function Expression, then it wouldn't work:
228229

229230
```js run refresh untrusted
230231
*!*
@@ -238,13 +239,11 @@ let sayHi = function(name) { // (*) no magic any more
238239

239240
Function Expressions are created when the execution reaches them. That would happen only in the line `(*)`. Too late.
240241

241-
**When a Function Declaration is made within a code block, it is visible everywhere inside that block. But not outside of it.**
242-
243-
Sometimes that's handy to declare a local function only needed in that block alone. But that feature may also cause problems.
242+
**In strict mode, when a Function Declaration is within a code block, it's visible everywhere inside that block. But not outside of it.**
244243

245244
For instance, let's imagine that we need to declare a function `welcome()` depending on the `age` variable that we get during runtime. And then we plan to use it some time later.
246245

247-
The code below doesn't work:
246+
If we use Function Declaration, it won't work as intended:
248247

249248
```js run
250249
let age = prompt("What is your age?", 18);
@@ -350,12 +349,12 @@ welcome(); // ok now
350349
```
351350

352351

353-
```smart header="When should you choose Function Declaration versus Function Expression?"
354-
As a rule of thumb, when we need to declare a function, the first to consider is Function Declaration syntax, the one we used before. It gives more freedom in how to organize our code, because we can call such functions before they are declared.
352+
```smart header="When to choose Function Declaration versus Function Expression?"
353+
As a rule of thumb, when we need to declare a function, the first to consider is Function Declaration syntax. It gives more freedom in how to organize our code, because we can call such functions before they are declared.
355354
356-
It's also a little bit easier to look up `function f(…) {…}` in the code than `let f = function(…) {…}`. Function Declarations are more "eye-catching".
355+
That's also better for readability, as it's easier to look up `function f(…) {…}` in the code than `let f = function(…) {…}`. Function Declarations are more "eye-catching".
357356
358-
...But if a Function Declaration does not suit us for some reason (we've seen an example above), then Function Expression should be used.
357+
...But if a Function Declaration does not suit us for some reason, or we need a conditional declaration (we've just seen an example), then Function Expression should be used.
359358
```
360359

361360

0 commit comments

Comments
 (0)