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
| Calling the `Number()`[^NumberFunction] concrete function (without `new` operator) is generally thought of as *merely* invoking the `ToNumber()` abstract operation to coerce a value to a number. While that's mostly true, it's not entirely so. `Number(42n)` works, whereas the abstract `ToNumber(42n)` itself throws an exception. |
185
185
186
+
#### Other Abstract Numeric Conversions
187
+
188
+
In addition to `ToNumber()`, the specification defines `ToNumeric()`, which is essentially invokes `ToPrimitive()` on a value, then conditionally invokes `ToNumber()` if the value is *not* already a `bigint` value-type.
189
+
190
+
There are also a wide variety of abstract operations related to converting values to very specific subsets of the general `number` type:
191
+
192
+
*`ToIntegerOrInfinity()`
193
+
*`ToInt32()`
194
+
*`ToUint32()`
195
+
*`ToInt16()`
196
+
*`ToUint16()`
197
+
*`ToInt8()`
198
+
*`ToUint8()`
199
+
*`ToUint8Clamp()`
200
+
201
+
Other operations related to `bigint`:
202
+
203
+
*`ToBigInt()`
204
+
*`StringToBigInt()`
205
+
*`ToBigInt64()`
206
+
*`ToBigUint64()`
207
+
208
+
You can probably infer the purpose of these operations from their names, and/or from consulting their algorithms in the specification. For most JS operations, it's more likely that a higher-level operation like `ToNumber()` is invoked, rather than these specific ones.
209
+
186
210
#### Default `valueOf()`
187
211
188
212
When `ToNumber()` is performed on object value-types, it instead invokes the `ToPrimitive()` operation (as explained earlier), with `"number"` as its *hinted* type:
@@ -212,6 +236,8 @@ By virtue of `ToPrimitive(..,"number")` delegation, these objects all have their
212
236
213
237
[^ToNumber]: "7.1.4 ToNumber(argument)", ECMAScript 2022 Language Specification; https://262.ecma-international.org/13.0/#sec-tonumber ; Accessed August 2022
214
238
239
+
[^ToNumeric]: "7.1.3 ToNumeric(argument)", ECMAScript 2022 Language Specification; https://262.ecma-international.org/13.0/#sec-tonumeric ; Accessed August 2022
240
+
215
241
[^NumberConstructor]: "21.1.1 The Number Constructor", ECMAScript 2022 Language Specification; https://262.ecma-international.org/13.0/#sec-number-constructor ; Accessed August 2022
216
242
217
243
[^NumberFunction]: "21.1.1.1 Number(value)", ECMAScript 2022 Language Specification; https://262.ecma-international.org/13.0/#sec-number-constructor-number-value ; Accessed August 2022
0 commit comments