This commit is contained in:
Ilya Kantor 2019-08-29 20:40:38 +03:00
parent 9f010c6c5e
commit df3260a44e
3 changed files with 6 additions and 1 deletions

View file

@ -14,6 +14,7 @@ true + false = 1
" -9 " - 5 = -14 // (4) " -9 " - 5 = -14 // (4)
null + 1 = 1 // (5) null + 1 = 1 // (5)
undefined + 1 = NaN // (6) undefined + 1 = NaN // (6)
" \t \n" - 2 = -2 // (7)
``` ```
1. The addition with a string `"" + 1` converts `1` to a string: `"" + 1 = "1"`, and then we have `"1" + 0`, the same rule is applied. 1. The addition with a string `"" + 1` converts `1` to a string: `"" + 1 = "1"`, and then we have `"1" + 0`, the same rule is applied.
@ -22,3 +23,4 @@ undefined + 1 = NaN // (6)
4. The subtraction always converts to numbers, so it makes `" -9 "` a number `-9` (ignoring spaces around it). 4. The subtraction always converts to numbers, so it makes `" -9 "` a number `-9` (ignoring spaces around it).
5. `null` becomes `0` after the numeric conversion. 5. `null` becomes `0` after the numeric conversion.
6. `undefined` becomes `NaN` after the numeric conversion. 6. `undefined` becomes `NaN` after the numeric conversion.
7. Space characters, such as `\t` and `\n`, are trimmed off string start and end when a string is converted to a number. So a string `\t \n`, similarly to an empty string, becomes `0`.

View file

@ -21,6 +21,7 @@ true + false
" -9 " - 5 " -9 " - 5
null + 1 null + 1
undefined + 1 undefined + 1
" \t \n" - 2
``` ```
Think well, write down and then compare with the answer. Think well, write down and then compare with the answer.

View file

@ -119,7 +119,7 @@ The method [arr.slice](mdn:js/Array/slice) is much simpler than similar-looking
The syntax is: The syntax is:
```js ```js
arr.slice(start, end) arr.slice([start], [end])
``` ```
It returns a new array copying to it all items from index `start` to `end` (not including `end`). Both `start` and `end` can be negative, in that case position from array end is assumed. It returns a new array copying to it all items from index `start` to `end` (not including `end`). Both `start` and `end` can be negative, in that case position from array end is assumed.
@ -136,6 +136,8 @@ alert( arr.slice(1, 3) ); // e,s (copy from 1 to 3)
alert( arr.slice(-2) ); // s,t (copy from -2 till the end) alert( arr.slice(-2) ); // s,t (copy from -2 till the end)
``` ```
We can also call it without arguments: `arr.slice()` creates a copy of `arr`. That's often used to obtain a copy for further transformations that should not affect the original array.
### concat ### concat
The method [arr.concat](mdn:js/Array/concat) creates a new array that includes values from other arrays and additional items. The method [arr.concat](mdn:js/Array/concat) creates a new array that includes values from other arrays and additional items.