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/02-first-steps/15-function-expressions-arrows/article.md
+13-14Lines changed: 13 additions & 14 deletions
Original file line number
Diff line number
Diff line change
@@ -196,19 +196,20 @@ First, the syntax: how to differentiate between them in the code.
196
196
197
197
The more subtle difference is *when* a function is created by the JavaScript engine.
198
198
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.**
200
200
201
201
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.
202
202
203
203
Function Declarations are different.
204
204
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.**
206
206
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.
208
208
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.
210
212
211
-
As a result, a function declared as a Function Declaration can be called earlier than it is defined.
212
213
213
214
For example, this works:
214
215
@@ -224,7 +225,7 @@ function sayHi(name) {
224
225
225
226
The Function Declaration `sayHi` is created when JavaScript is preparing to start the script and is visible everywhere in it.
226
227
227
-
...If it was a Function Expression, then it wouldn't work:
228
+
...If it were a Function Expression, then it wouldn't work:
228
229
229
230
```js run refresh untrusted
230
231
*!*
@@ -238,13 +239,11 @@ let sayHi = function(name) { // (*) no magic any more
238
239
239
240
Function Expressions are created when the execution reaches them. That would happen only in the line `(*)`. Too late.
240
241
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.**
244
243
245
244
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.
246
245
247
-
The code below doesn't work:
246
+
If we use Function Declaration, it won't work as intended:
248
247
249
248
```js run
250
249
let age =prompt("What is your age?", 18);
@@ -350,12 +349,12 @@ welcome(); // ok now
350
349
```
351
350
352
351
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.
355
354
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".
357
356
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.
0 commit comments