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/2-first-steps/08-type-conversions/article.md
+27-37Lines changed: 27 additions & 37 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -32,6 +32,26 @@ alert(typeof a); // string
32
32
33
33
The string conversion is mostly obvious. A `false` becomes `"false"`, `null` becomes `"null"` etc.
34
34
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
+
35
55
## ToNumber
36
56
37
57
Numeric conversion happens in mathematical functions and expressions automatically.
@@ -137,46 +157,15 @@ alert( Boolean({}) ); // true, even if empty object
137
157
```
138
158
````
139
159
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
-
173
160
## Summary
174
161
175
162
There exist three most widely used type conversions: to string, to number and to boolean.
176
163
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.
178
166
179
-
**ToNumber** follows the rules:
167
+
ToNumber
168
+
: ToNumber follows the rules:
180
169
181
170
| Value | Becomes... |
182
171
|-------|-------------|
@@ -185,17 +174,18 @@ There exist three most widely used type conversions: to string, to number and to
185
174
|<code>true / false</code> |`1 / 0`|
186
175
|`string`| The string is read "as is", whitespaces from both sides are ignored. An empty string is `0`. An error gives `NaN`. |
187
176
188
-
**ToBoolean** is the simplest one:
177
+
ToBoolean
178
+
: ToBoolean is the simplest one:
189
179
190
180
| Value | Becomes... |
191
181
|-------|-------------|
192
182
|`0`, `null`, `undefined`, `NaN`, `""`|`false`|
193
183
|any other value|`true`|
194
184
185
+
195
186
Most of these rules are easy to understand and memorize. The notable exceptions where people usually make mistakes are:
196
187
197
188
-`undefined` is `NaN` as a number.
198
189
-`"0"` is true as a boolean.
199
190
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.
0 commit comments