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, then we need an explicit "return"
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!
98
+
99
+
Arrow functions have other interesting features.
100
+
101
+
To study them in-depth, we first need to get to know some other aspects of JavaScript, so we'll return to arrow functions later in the chapter <info:arrow-functions>.
102
+
103
+
For now, we can already use arrow functions for one-line actions and callbacks.
104
+
```
105
+
106
+
## Summary
107
+
108
+
Arrow functions are handy for one-liners. They come in two flavors:
109
+
110
+
1. Without curly braces: `(...args) => expression` -- the right side is an expression: the function evaluates it and returns the result.
111
+
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.
Copy file name to clipboardexpand all lines: 1-js/05-data-types/02-number/article.md
+1-1
Original file line number
Diff line number
Diff line change
@@ -201,7 +201,7 @@ Strange! What is it then if not `0.3`?
201
201
alert( 0.1 + 0.2 ); // 0.30000000000000004
202
202
```
203
203
204
-
Ouch! There are more consequences than an incorrect comparison here. Imagine you're making an e-shopping site and the visitor puts `$0.10` and `$0.20` goods into their chart. The order total will be `$0.30000000000000004`. That would surprise anyone.
204
+
Ouch! There are more consequences than an incorrect comparison here. Imagine you're making an e-shopping site and the visitor puts `$0.10` and `$0.20` goods into their cart. The order total will be `$0.30000000000000004`. That would surprise anyone.
Copy file name to clipboardexpand all lines: 1-js/05-data-types/11-date/article.md
+7
Original file line number
Diff line number
Diff line change
@@ -33,6 +33,13 @@ To create a new `Date` object call `new Date()` with one of the following argume
33
33
34
34
It's a lightweight numeric representation of a date. We can always create a date from a timestamp using `new Date(timestamp)` and convert the existing `Date` object to a timestamp using the `date.getTime()` method (see below).
35
35
36
+
Dates before 01.01.1970 have negative timestamps, e.g.:
37
+
```js run
38
+
// 31 Dec 1969
39
+
let Dec31_1969 = new Date(-24 * 3600 * 1000);
40
+
alert( Dec31_1969 );
41
+
```
42
+
36
43
`new Date(datestring)`
37
44
: If there is a single argument, and it's a string, then it is parsed automatically. The algorithm is the same as `Date.parse` uses, we'll cover it later.
Sometimes people say that `class` is a "syntax sugar" (syntax that is designed to make things easier to read, but doesn't introduce anything new), because we could actually declare the same without `class` keyword at all:
121
+
Sometimes people say that `class` is a "syntactic sugar" (syntax that is designed to make things easier to read, but doesn't introduce anything new), because we could actually declare the same without `class` keyword at all:
122
122
123
123
```js run
124
124
// rewriting class User in pure functions
@@ -140,7 +140,7 @@ let user = new User("John");
140
140
user.sayHi();
141
141
```
142
142
143
-
The result of this definition is about the same. So, there are indeed reasons why `class` can be considered a syntax sugar to define a constructor together with its prototype methods.
143
+
The result of this definition is about the same. So, there are indeed reasons why `class` can be considered a syntactic sugar to define a constructor together with its prototype methods.
Here's an example with a computed property in brackets `[...]`:
285
+
Here's an example with a computed property name in brackets `[...]`:
286
286
287
287
```js run
288
288
classUser {
@@ -318,6 +318,9 @@ class User {
318
318
}
319
319
320
320
newUser().sayHi();
321
+
322
+
alert(User.prototype.sayHi); // placed in User.prototype
323
+
alert(User.prototype.name); // undefined, not placed in User.prototype
321
324
```
322
325
323
326
The property `name` is not placed into `User.prototype`. Instead, it is created by `new` before calling the constructor, it's a property of the object itself.
0 commit comments