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
+29-29Lines changed: 29 additions & 29 deletions
Original file line number
Diff line number
Diff line change
@@ -1,18 +1,18 @@
1
1
# Function expressions and arrows
2
2
3
-
In JavaScript a function is not a "magical language structure", but a special kind of value.
3
+
In JavaScript, a function is not a "magical language structure", but a special kind of value.
4
4
5
5
[cut]
6
6
7
-
The syntax that we used before is called *Function Declaration*:
7
+
The syntax that we used before is called a *Function Declaration*:
8
8
9
9
```js
10
10
functionsayHi() {
11
11
alert( "Hello" );
12
12
}
13
13
```
14
14
15
-
There is another syntax of creating a function that is called *Function Expression*.
15
+
There is another syntax for creating a function that is called a*Function Expression*.
16
16
17
17
It looks like this:
18
18
@@ -22,7 +22,7 @@ let sayHi = function() {
22
22
};
23
23
```
24
24
25
-
Here the function is created and assigned to the variable explicitly, like any other value. No matter how the function is defined, it's just a value stored in the variable `sayHi`.
25
+
Here, the function is created and assigned to the variable explicitly, like any other value. No matter how the function is defined, it's just a value stored in the variable `sayHi`.
26
26
27
27
28
28
The meaning of these code samples is the same: "create a function and put it into the variable `sayHi`".
@@ -41,7 +41,7 @@ alert( sayHi ); // shows the function code
41
41
42
42
Please note that the last line does not run the function, because there are no parentheses after `sayHi`. There are programming languages where any mention of a function name causes its execution, but JavaScript is not like that.
43
43
44
-
In JavaScript, a function is a value and we can deal with it as a value. The code above shows its string representation, that is the source code.
44
+
In JavaScript, a function is a value, so we can deal with it as a value. The code above shows its string representation, which is the source code.
45
45
46
46
It is a special value of course, in the sense that we can call it like `sayHi()`.
47
47
@@ -62,13 +62,13 @@ sayHi(); // Hello // this still works too (why wouldn't it)
62
62
63
63
Here's what happens above in detail:
64
64
65
-
1. Function Declaration `(1)` creates the function and puts it into the variable named `sayHi`.
66
-
2. Line `(2)` copies it into variable `func`.
65
+
1.The Function Declaration `(1)` creates the function and puts it into the variable named `sayHi`.
66
+
2. Line `(2)` copies it into the variable `func`.
67
67
68
-
Please note again: there are no parentheses after `sayHi`. If they were, then `func = sayHi()` would write *the result of the call*`sayHi()` into `func`, not *the function*`sayHi` itself.
69
-
3. Now the function can be called both as`sayHi()` and `func()`.
68
+
Please note again: there are no parentheses after `sayHi`. If there were, then `func = sayHi()` would write *the result of the call*`sayHi()` into `func`, not *the function*`sayHi` itself.
69
+
3. Now the function can be called as both`sayHi()` and `func()`.
70
70
71
-
Note, that we could also have used a Function Expression to declare `sayHi`, in the first line:
71
+
Note that we could also have used a Function Expression to declare `sayHi`, in the first line:
72
72
73
73
```js
74
74
letsayHi=function() { ... };
@@ -100,7 +100,7 @@ The answer is simple:
100
100
101
101
## Callback functions
102
102
103
-
Let's see more examples of passing functions as values and using function expressions.
103
+
Let's look at more examples of passing functions as values and using function expressions.
104
104
105
105
We'll write a function `ask(question, yes, no)` with three parameters:
106
106
@@ -137,13 +137,13 @@ ask("Do you agree?", showOk, showCancel);
137
137
138
138
Before we explore how we can write it in a much shorter way, let's note that in the browser (and on the server-side in some cases) such functions are quite popular.
139
139
140
-
The major difference between a real-life implementation and the example above is that real-life functions use more complex ways to interact with the user than a simple `confirm`. In the browser such a function usually draws a nice-looking question window. But that's another story.
140
+
The major difference between a real-life implementation and the example above is that real-life functions use more complex ways to interact with the user than a simple `confirm`. In the browser, such a function usually draws a nice-looking question window. But that's another story.
141
141
142
142
**The arguments of `ask` are called *callback functions* or just *callbacks*. The idea is that we pass a function and expect it to be "called back" in certain circumstances.**
143
143
144
144
So, `showOk` becomes the callback for the "yes" answer, and `showCancel` for the "no" answer.
145
145
146
-
We can use Function Expressions to write the same much shorter:
146
+
We can use Function Expressions to write the same function much shorter:
147
147
148
148
```js run no-beautify
149
149
functionask(question, yes, no) {
@@ -160,7 +160,7 @@ ask(
160
160
*/!*
161
161
```
162
162
163
-
Here functions are declared right inside the `ask(...)` call. They have no name, and so are called *anonymous*. Such functions are not accessible outside of `ask` (because they are not assigned to variables), but that's just what we want here.
163
+
Here, functions are declared right inside the `ask(...)` call. They have no name, and so are called *anonymous*. Such functions are not accessible outside of `ask` (because they are not assigned to variables), but that's just what we want here.
164
164
165
165
Such code appears in our scripts very naturally, it's in the spirit of JavaScript.
166
166
@@ -190,7 +190,7 @@ First, the syntax: how to see what is what in the code.
190
190
```
191
191
-*Function Expression:* a function, created inside an expression or inside another syntax construct.
192
192
193
-
Here the function is created at the right side of the "assignment expression =":
193
+
Here, the function is created at the right side of the "assignment expression =":
194
194
```js
195
195
// Function Expression
196
196
let sum = function(a, b) {
@@ -200,19 +200,19 @@ First, the syntax: how to see what is what in the code.
200
200
201
201
The more subtle difference is *when* a function is created by the JavaScript engine.
202
202
203
-
**Function Expression is created when the execution reaches it and is usable from then on.**
203
+
**A Function Expression is created when the execution reaches it and is usable from then on.**
204
204
205
205
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, calledetc) from now on.
206
206
207
207
Function Declarations are different.
208
208
209
-
**Function Declaration is usable in the whole script/code block.**
209
+
**A Function Declaration is usable in the whole script/code block.**
210
210
211
211
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".
212
212
213
-
And after all Function Declarations are processed, the execution goes on.
213
+
And after all of the Function Declarations are processed, the execution goes on.
214
214
215
-
As a natural effect, a function declared as Function Declaration can be called earlier than it is defined.
215
+
As a result, a function declared as a Function Declaration can be called earlier than it is defined.
216
216
217
217
For example, this works:
218
218
@@ -228,7 +228,7 @@ function sayHi(name) {
228
228
229
229
The Function Declaration `sayHi` is created when JavaScript is preparing to start the script and is visible everywhere in it.
230
230
231
-
...And if there were Function Expression, then it wouldn't work:
231
+
...If it was a Function Expression, then it wouldn't work:
232
232
233
233
```js run refresh untrusted
234
234
*!*
@@ -275,7 +275,7 @@ welcome(); // Error: welcome is not defined
275
275
*/!*
276
276
```
277
277
278
-
A Function Declaration is visible only inside the code block where it resides.
278
+
A Function Declaration is only visible inside the code block in which it resides.
279
279
280
280
We can call it from within the block, but not from outside:
281
281
@@ -313,7 +313,7 @@ welcome(); // Error: welcome is not defined
313
313
314
314
What can we do to make `welcome` visible outside of `if`?
315
315
316
-
The right thing would be to use a Function Expression and assign `welcome` to the variable which is declared outside of `if` and has the proper visibility:
316
+
The correct approach would be to use a Function Expression and assign `welcome` to the variable that is declared outside of `if` and has the proper visibility:
317
317
318
318
```js run
319
319
let age =prompt("What is your age?", 18);
@@ -359,13 +359,13 @@ As a rule of thumb, when we need to declare a function, the first to consider is
359
359
360
360
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".
361
361
362
-
...But if Function Declaration does not suit us for some reason (we've seen an example above), then Function Expression should be used.
362
+
...But if a Function Declaration does not suit us for some reason (we've seen an example above), then Function Expression should be used.
363
363
```
364
364
365
365
366
366
## Arrow functions [#arrow-functions]
367
367
368
-
There's one more syntax for creating functions -- very simple and concise. It's called "arrow functions", because it looks like this:
368
+
There's one more very simple and concise syntax for creating functions. It's called "arrow functions", because it looks like this:
If there are no arguments, we can put empty parentheses:
415
+
If there are no arguments, we can insert empty parentheses:
416
416
417
417
```js run
418
418
letsayHi= () =>alert("Hello!");
419
419
420
420
sayHi();
421
421
```
422
422
423
-
Arrow functions can be used the same way as Function Expressions.
423
+
Arrow functions can be used in the same way as Function Expressions.
424
424
425
425
For instance, here's the rewritten example with `welcome()`:
426
426
@@ -472,9 +472,9 @@ For now, we can already use them for one-line actions and callbacks.
472
472
- Function Expressions are created when the execution flow reaches them.
473
473
474
474
475
-
In most cases when we need to declare a function, Function Declaration is preferable, because it is visible prior to the declaration itself. That gives us more flexibility in code organization. And is usually more readable.
475
+
In most cases when we need to declare a function, a Function Declaration is preferable, because it is visible prior to the declaration itself. That gives us more flexibility in code organization, and is usually more readable.
476
476
477
-
So we should use Function Expression only when Function Declaration does not fit the task. We've seen a couple of examples of that in the chapter, and will see more in the future.
477
+
So we should use a Function Expression only when a Function Declaration is not fit for the task. We've seen a couple of examples of that in this chapter, and will see more in the future.
478
478
479
479
Arrow functions are handy for one-liners. They come in two flavors:
0 commit comments