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/article.md
+1-107
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,4 @@
1
-
# Function expressions and arrows
1
+
# Function expressions
2
2
3
3
In JavaScript, a function is not a "magical language structure", but a special kind of value.
4
4
@@ -355,107 +355,6 @@ That's also better for readability, as it's easier to look up `function f(…) {
355
355
...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.
356
356
```
357
357
358
-
359
-
## Arrow functions [#arrow-functions]
360
-
361
-
There's one more very simple and concise syntax for creating functions, that's often better than Function Expressions. It's called "arrow functions", because it looks like this:
362
-
363
-
364
-
```js
365
-
letfunc= (arg1, arg2, ...argN) => expression
366
-
```
367
-
368
-
...This creates a function `func` that has arguments `arg1..argN`, evaluates the `expression` on the right side with their use and returns its result.
369
-
370
-
In other words, it's roughly the same as:
371
-
372
-
```js
373
-
letfunc=function(arg1, arg2, ...argN) {
374
-
return expression;
375
-
};
376
-
```
377
-
378
-
...But much more concise.
379
-
380
-
Let's see an example:
381
-
382
-
```js run
383
-
letsum= (a, b) => a + b;
384
-
385
-
/* The arrow function is a shorter form of:
386
-
387
-
let sum = function(a, b) {
388
-
return a + b;
389
-
};
390
-
*/
391
-
392
-
alert( sum(1, 2) ); // 3
393
-
394
-
```
395
-
396
-
If we have only one argument, then parentheses around parameters can be omitted, making that even shorter:
397
-
398
-
```js run
399
-
// same as
400
-
// let double = function(n) { return n * 2 }
401
-
*!*
402
-
letdouble=n=> n *2;
403
-
*/!*
404
-
405
-
alert( double(3) ); // 6
406
-
```
407
-
408
-
If there are no arguments, parentheses should be empty (but they should be present):
409
-
410
-
```js run
411
-
letsayHi= () =>alert("Hello!");
412
-
413
-
sayHi();
414
-
```
415
-
416
-
Arrow functions can be used in the same way as Function Expressions.
417
-
418
-
For instance, here's the rewritten example with `welcome()`:
419
-
420
-
```js run
421
-
let age =prompt("What is your age?", 18);
422
-
423
-
let welcome = (age <18) ?
424
-
() =>alert('Hello') :
425
-
() =>alert("Greetings!");
426
-
427
-
welcome(); // ok now
428
-
```
429
-
430
-
Arrow functions may appear unfamiliar and not very readable at first, but that quickly changes as the eyes get used to the structure.
431
-
432
-
They are very convenient for simple one-line actions, when we're just too lazy to write many words.
433
-
434
-
```smart header="Multiline arrow functions"
435
-
436
-
The examples above took arguments from the left of `=>` and evaluated the right-side expression with them.
437
-
438
-
Sometimes we need something a little bit more complex, like multiple expressions or statements. It is also possible, but we should enclose them in curly braces. Then use a normal `return` within them.
439
-
440
-
Like this:
441
-
442
-
```js run
443
-
let sum = (a, b) => { // the curly brace opens a multiline function
444
-
let result = a + b;
445
-
*!*
446
-
return result; // if we use curly braces, use return to get results
447
-
*/!*
448
-
};
449
-
450
-
alert( sum(1, 2) ); // 3
451
-
```
452
-
453
-
```smart header="More to come"
454
-
Here we praised arrow functions for brevity. But that's not all! Arrow functions have other interesting features. We'll return to them later in the chapter <info:arrow-functions>.
455
-
456
-
For now, we can already use arrow functions for one-line actions and callbacks.
457
-
```
458
-
459
358
## Summary
460
359
461
360
- Functions are values. They can be assigned, copied or declared in any place of the code.
@@ -467,8 +366,3 @@ For now, we can already use arrow functions for one-line actions and callbacks.
467
366
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.
468
367
469
368
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.
470
-
471
-
Arrow functions are handy for one-liners. They come in two flavors:
472
-
473
-
1. Without curly braces: `(...args) => expression` -- the right side is an expression: the function evaluates it and returns the result.
474
-
2. With curly braces: `(...args) => { body }` -- brackets allow us to write multiple statements inside the function, but we need an explicit `return` to return something.
There's another very simple and concise syntax for creating functions, that's often better than Function Expressions.
4
+
5
+
It's called "arrow functions", because it looks like this:
6
+
7
+
```js
8
+
letfunc= (arg1, arg2, ...argN) => expression
9
+
```
10
+
11
+
...This creates a function `func` that accepts arguments `arg1..argN`, then evaluates the `expression` on the right side with their use and returns its result.
12
+
13
+
In other words, it's the shorter version of:
14
+
15
+
```js
16
+
letfunc=function(arg1, arg2, ...argN) {
17
+
return expression;
18
+
};
19
+
```
20
+
21
+
Let's see a concrete example:
22
+
23
+
```js run
24
+
letsum= (a, b) => a + b;
25
+
26
+
/* This arrow function is a shorter form of:
27
+
28
+
let sum = function(a, b) {
29
+
return a + b;
30
+
};
31
+
*/
32
+
33
+
alert( sum(1, 2) ); // 3
34
+
```
35
+
36
+
As you can, see `(a, b) => a + b` means a function that accepts two arguments named `a` and `b`. Upon the execution, it evaluates the expression `a + b` and returns the result.
37
+
38
+
- If we have only one argument, then parentheses around parameters can be omitted, making that even shorter.
39
+
40
+
For example:
41
+
42
+
```js run
43
+
*!*
44
+
letdouble=n=> n *2;
45
+
// roughly the same as: let double = function(n) { return n * 2 }
46
+
*/!*
47
+
48
+
alert( double(3) ); // 6
49
+
```
50
+
51
+
- If there are no arguments, parentheses will be empty (but they should be present):
52
+
53
+
```js run
54
+
let sayHi = () => alert("Hello!");
55
+
56
+
sayHi();
57
+
```
58
+
59
+
Arrow functions can be used in the same way as Function Expressions.
60
+
61
+
For instance, to dynamically create a function:
62
+
63
+
```js run
64
+
let age = prompt("What is your age?", 18);
65
+
66
+
let welcome = (age < 18) ?
67
+
() => alert('Hello') :
68
+
() => alert("Greetings!");
69
+
70
+
welcome(); // ok now
71
+
```
72
+
73
+
Arrow functions may appear unfamiliar and not very readable at first, but that quickly changes as the eyes get used to the structure.
74
+
75
+
They are very convenient for simple one-line actions, when we're just too lazy to write many words.
76
+
77
+
## Multiline arrow functions
78
+
79
+
The examples above took arguments from the left of `=>` and evaluated the right-side expression with them.
80
+
81
+
Sometimes we need something a little bit more complex, like multiple expressions or statements. It is also possible, but we should enclose them in curly braces. Then use a normal `return` within them.
82
+
83
+
Like this:
84
+
85
+
```js run
86
+
let sum = (a, b) => { // the curly brace opens a multiline function
87
+
let result = a + b;
88
+
*!*
89
+
return result; // if we use curly braces, use return to get results
90
+
*/!*
91
+
};
92
+
93
+
alert( sum(1, 2) ); // 3
94
+
```
95
+
96
+
```smart header="More to come"
97
+
Here we praised arrow functions for brevity. But that's not all! Arrow functions have other interesting features. We'll return to them later in the chapter <info:arrow-functions>.
98
+
99
+
For now, we can already use arrow functions for one-line actions and callbacks.
100
+
```
101
+
102
+
## Summary
103
+
104
+
Arrow functions are handy for one-liners. They come in two flavors:
105
+
106
+
1. Without curly braces: `(...args) => expression` -- the right side is an expression: the function evaluates it and returns the result.
107
+
2. With curly braces: `(...args) => { body }` -- brackets allow us to write multiple statements inside the function, but we need an explicit `return` to return something.
0 commit comments