Update 02-first-steps/06-type-conversions

This commit is contained in:
imidom 2018-12-29 21:18:29 -05:00 committed by GitHub
parent f2bece38d0
commit 5e6ad1eca5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,13 +1,13 @@
# Type Conversions
Most of the time, operators and functions automatically convert a value to the right type. That's called "type conversion".
Most of the time, operators and functions automatically convert the values given to them to the right type. This is called "type conversion".
For example, `alert` automatically converts any value to a string to show it. Mathematical operations convert values to numbers.
There are also cases when we need to explicitly convert a value to put things right.
There are also cases when we need to explicitly convert a value to the expected type.
```smart header="Not talking about objects yet"
In this chapter we don't cover objects yet. Here we study primitives first. Later, after we learn objects, we'll see how object conversion works in the chapter <info:object-toprimitive>.
In this chapter, we won't cover objects. Instead, we'll study primitives first. Later, after we learn about objects, we'll see how object conversion works in the chapter <info:object-toprimitive>.
```
## ToString
@ -16,7 +16,7 @@ String conversion happens when we need the string form of a value.
For example, `alert(value)` does it to show the value.
We can also use a call `String(value)` function for that:
We can also call the `String(value)` function to convert a value to a string:
```js run
let value = true;
@ -28,7 +28,7 @@ alert(typeof value); // string
*/!*
```
String conversion is mostly obvious. A `false` becomes `"false"`, `null` becomes `"null"` etc.
String conversion is mostly obvious. A `false` becomes `"false"`, `null` becomes `"null"`, etc.
## ToNumber
@ -40,7 +40,7 @@ For example, when division `/` is applied to non-numbers:
alert( "6" / "2" ); // 3, strings are converted to numbers
```
We can use a `Number(value)` function to explicitly convert a `value`:
We can use the `Number(value)` function to explicitly convert a `value` to a number:
```js run
let str = "123";
@ -51,9 +51,9 @@ let num = Number(str); // becomes a number 123
alert(typeof num); // number
```
Explicit conversion is usually required when we read a value from a string-based source like a text form, but we expect a number to be entered.
Explicit conversion is usually required when we read a value from a string-based source like a text form but expect a number to be entered.
If the string is not a valid number, the result of such conversion is `NaN`, for instance:
If the string is not a valid number, the result of such a conversion is `NaN`. For instance:
```js run
let age = Number("an arbitrary string instead of a number");
@ -68,7 +68,7 @@ Numeric conversion rules:
|`undefined`|`NaN`|
|`null`|`0`|
|<code>true&nbsp;and&nbsp;false</code> | `1` and `0` |
| `string` | Whitespaces from the start and the end are removed. Then, if the remaining string is empty, the result is `0`. Otherwise, the number is "read" from the string. An error gives `NaN`. |
| `string` | Whitespaces from the start and end are removed. If the remaining string is empty, the result is `0`. Otherwise, the number is "read" from the string. An error gives `NaN`. |
Examples:
@ -79,30 +79,30 @@ alert( Number(true) ); // 1
alert( Number(false) ); // 0
```
Please note that `null` and `undefined` behave differently here: `null` becomes a zero, while `undefined` becomes `NaN`.
Please note that `null` and `undefined` behave differently here: `null` becomes zero while `undefined` becomes `NaN`.
````smart header="Addition '+' concatenates strings"
Almost all mathematical operations convert values to numbers. With a notable exception of the addition `+`. If one of the added values is a string then the other one is also converted to a string.
Almost all mathematical operations convert values to numbers. A notable exception is addition `+`. If one of the added values is a string, the other one is also converted to a string.
Then it concatenates (joins) them:
Then, it concatenates (joins) them:
```js run
alert( 1 + '2' ); // '12' (string to the right)
alert( '1' + 2 ); // '12' (string to the left)
```
That only happens when at least one of the arguments is a string. Otherwise, values are converted to numbers.
This only happens when at least one of the arguments is a string. Otherwise, values are converted to numbers.
````
## ToBoolean
Boolean conversion is the simplest one.
It happens in logical operations (later we'll meet condition tests and other kinds of them), but also can be performed manually with the call of `Boolean(value)`.
It happens in logical operations (later we'll meet condition tests and other similar things) but can also be performed explicitly with a call to `Boolean(value)`.
The conversion rule:
- Values that are intuitively "empty", like `0`, an empty string, `null`, `undefined` and `NaN` become `false`.
- Values that are intuitively "empty", like `0`, an empty string, `null`, `undefined`, and `NaN`, become `false`.
- Other values become `true`.
For instance:
@ -116,7 +116,7 @@ alert( Boolean("") ); // false
```
````warn header="Please note: the string with zero `\"0\"` is `true`"
Some languages (namely PHP) treat `"0"` as `false`. But in JavaScript a non-empty string is always `true`.
Some languages (namely PHP) treat `"0"` as `false`. But in JavaScript, a non-empty string is always `true`.
```js run
alert( Boolean("0") ); // true
@ -127,11 +127,11 @@ alert( Boolean(" ") ); // spaces, also true (any non-empty string is true)
## Summary
The three most widely used type conversions are: to string, to number and to boolean.
The three most widely used type conversions are to string, to number, and to boolean.
**`ToString`** -- Occurs when we output something, can be performed with `String(value)`. The conversion to string is usually obvious for primitive values.
**`ToString`** -- Occurs when we output something. Can be performed with `String(value)`. The conversion to string is usually obvious for primitive values.
**`ToNumber`** -- Occurs in math operations, can be performed with `Number(value)`.
**`ToNumber`** -- Occurs in math operations. Can be performed with `Number(value)`.
The conversion follows the rules:
@ -142,7 +142,7 @@ The conversion follows the rules:
|<code>true&nbsp;/&nbsp;false</code> | `1 / 0` |
| `string` | The string is read "as is", whitespaces from both sides are ignored. An empty string becomes `0`. An error gives `NaN`. |
**`ToBoolean`** -- Occurs in logical operations, or can be performed with `Boolean(value)`.
**`ToBoolean`** -- Occurs in logical operations. Can be performed with `Boolean(value)`.
Follows the rules:
@ -157,4 +157,4 @@ Most of these rules are easy to understand and memorize. The notable exceptions
- `undefined` is `NaN` as a number, not `0`.
- `"0"` and space-only strings like `" "` are true as a boolean.
Objects are not covered here, we'll return to them later in the chapter <info:object-toprimitive> that is devoted exclusively to objects, after we learn more basic things about JavaScript.
Objects aren't covered here. We'll return to them later in the chapter <info:object-toprimitive> that is devoted exclusively to objects after we learn more basic things about JavaScript.