Skip to content

Commit 8b17058

Browse files
committed
refactor
1 parent 3451b31 commit 8b17058

14 files changed

Lines changed: 318 additions & 715 deletions

File tree

1-js/2-first-steps/08-type-conversions/article.md

Lines changed: 27 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,26 @@ alert(typeof a); // string
3232

3333
The string conversion is mostly obvious. A `false` becomes `"false"`, `null` becomes `"null"` etc.
3434

35+
For objects, the string conversion can be customized, we'll study that in future. But there are two most common cases to know:
36+
37+
- A plain object becomes `[object Object]` when we output it or expicitly convert to a string:
38+
39+
```js run
40+
alert( {} ); // [object Object]
41+
alert( {name: "John"} ); // [object Object]
42+
```
43+
44+
- An array becomes a comma-delimited list of items when converted to a string:
45+
46+
```js run
47+
let arr = [1, 2, 3];
48+
49+
alert( arr ); // 1,2,3
50+
51+
alert( String(arr) === '1,2,3' ); // true
52+
```
53+
54+
3555
## ToNumber
3656

3757
Numeric conversion happens in mathematical functions and expressions automatically.
@@ -137,46 +157,15 @@ alert( Boolean({}) ); // true, even if empty object
137157
```
138158
````
139159

140-
141-
## ToPrimitive
142-
143-
If we convert an object to a string or a number, then it's a two-stage process.
144-
145-
1. The object is first converted to a primitive value.
146-
2. And then ToString/ToNumber rules are applied to it.
147-
148-
The conversion is customizable, we'll study it later when we go deeper into objects. [todo in the chapter?]
149-
150-
But for now let's see two most common cases.
151-
152-
- A plain object becomes `[object Object]` when we output it or expicitly convert to a string:
153-
154-
```js run
155-
alert( {} ); // [object Object]
156-
alert( {name: "John"} ); // [object Object]
157-
```
158-
159-
- An array becomes a comma-delimited list of items:
160-
161-
```js run
162-
let arr = [1, 2, 3];
163-
164-
alert( arr ); // 1,2,3 (implicit conversion)
165-
alert( String(arr) === '1,2,3' ); // true
166-
```
167-
168-
`ToBoolean` provides no customizability for objects. The rule is simple: all objects are truthy.
169-
170-
We'll return to object conversions it in the chapter [todo].
171-
172-
173160
## Summary
174161

175162
There exist three most widely used type conversions: to string, to number and to boolean.
176163

177-
**ToString** is usully obvious for primitive values, but depends on the object type for objects. For instance, arrays turn into a comma-delimited list of elements.
164+
ToString
165+
: ToString is usully obvious for primitive values, but depends on the object type for objects. For instance, arrays turn into a comma-delimited list of elements.
178166

179-
**ToNumber** follows the rules:
167+
ToNumber
168+
: ToNumber follows the rules:
180169

181170
| Value | Becomes... |
182171
|-------|-------------|
@@ -185,17 +174,18 @@ There exist three most widely used type conversions: to string, to number and to
185174
|<code>true&nbsp;/&nbsp;false</code> | `1 / 0` |
186175
| `string` | The string is read "as is", whitespaces from both sides are ignored. An empty string is `0`. An error gives `NaN`. |
187176

188-
**ToBoolean** is the simplest one:
177+
ToBoolean
178+
: ToBoolean is the simplest one:
189179

190180
| Value | Becomes... |
191181
|-------|-------------|
192182
|`0`, `null`, `undefined`, `NaN`, `""` |`false`|
193183
|any other value| `true` |
194184

185+
195186
Most of these rules are easy to understand and memorize. The notable exceptions where people usually make mistakes are:
196187

197188
- `undefined` is `NaN` as a number.
198189
- `"0"` is true as a boolean.
199190

200-
Objects can define their own methods of converting to a string or a number, we'll see them later. But they can't redefine the conversion to boolean.
201191

0 commit comments

Comments
 (0)