Skip to content

Commit 9fd7300

Browse files
committed
types-grammar, ch4: mentioning the other numeric abstract coercion operations
1 parent 591e933 commit 9fd7300

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

types-grammar/ch4.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,30 @@ ToNumber(Symbol("42")); // TypeError exception thrown
183183
| :--- |
184184
| 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. |
185185

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+
186210
#### Default `valueOf()`
187211

188212
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
212236

213237
[^ToNumber]: "7.1.4 ToNumber(argument)", ECMAScript 2022 Language Specification; https://262.ecma-international.org/13.0/#sec-tonumber ; Accessed August 2022
214238

239+
[^ToNumeric]: "7.1.3 ToNumeric(argument)", ECMAScript 2022 Language Specification; https://262.ecma-international.org/13.0/#sec-tonumeric ; Accessed August 2022
240+
215241
[^NumberConstructor]: "21.1.1 The Number Constructor", ECMAScript 2022 Language Specification; https://262.ecma-international.org/13.0/#sec-number-constructor ; Accessed August 2022
216242

217243
[^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

Comments
 (0)