
JavaScript: Want to know why sometimes ∞ is 0 or ∞ is maxValue?
Numbers are treated differently in JavaScript in different methods. Sometimes ∞ becomes 0, sometimes Infinity becomes maxValue. The abstract operation the method uses (of Object/Type/Function) determines the outcome.
ToIntegerOrInfinity ( argument )
ToIntegerOrInfinity is an abstract operation and takes argument argument. The argument is converted to an integer, +∞, or -∞. The algorithm or rules it follows are listed below:
- Let number be ? ToNumber(argument).
- If number is NaN, +0𝔽, or -0𝔽, return 0.
- If number is +∞𝔽, return +∞.
- If number is -∞𝔽, return -∞.
- Let integer be floor(abs(ℝ(number))).
- If number < +0𝔽, set integer to -integer.
- Return integer.
Reference: ECMAScript Language Specification
The Object/type/function that uses ToIntegerOrInfinity are listed below:
| Object / Type / Function | Integer |
|---|---|
| String | charAt |
| charCodeAt | |
| codePointAt | |
| endsWith | |
| includes | |
| indexOf | |
| lastIndexOf | |
| repeat | |
| slice | |
| startsWith | |
| subString | |
| Array | slice |
| splice | |
| copyWithin | |
| fill | |
| flat | |
| includes | |
| indexOf | |
| lastIndexOf | |
| TypedArray | copyWithin |
| fill | |
| includes | |
| indexOf | |
| lastIndexOf | |
| set | |
| slice | |
| subarray | |
| SharedArrayBuffer | slice |
| Atomics | AtomicReadModifyWrite |
| compareExchange | |
| isLockFree | |
| store | |
| notify | |
| Date | setYear |
| UTC | |
| MakeTime (future) | |
| MakeDay (future) | |
| TimeClip (future) | |
| Date Constructor | Date(…Values) |
| Abstract Operations | ToLength |
| ToIndex | |
| String Exotic Objects | [[OwnPropertyKeys]] ( ) |
| Function | bind |
| Number | toExponential |
| toFixed | |
| toPrecision | |
| toString | |
| BigInt | toString |
SameValueZero ( x,y )
SameValueZero is an abstract operation and takes arguments x and y; where x and y are ECMAScript language value and returns [[Value]] as Boolean and [[Type]] as normal. The algorithm or rules it follows are listed below:
- If Type(x) is different from Type(y), return false.
- If Type(x) is Number or BigInt, then a. Return ! Type(x)::sameValueZero(x, y).
- Return ! SameValueNonNumeric(x, y).
SameValueZerodiffers fromSameValueonly in the way how it treats +0𝔽 and -0𝔽.
Reference: ECMAScript Language Specification
The Object/type that uses SameValueZero are listed below:
| Object / Type | SameValueZero |
|---|---|
| Number Type | Number::sameValueZero |
| BigInt Type | BigInt::sameValueZero |
| Array | includes |
| TypedArray | includes |
| Map | delete |
| get | |
| has | |
| set | |
| Set | add |
| constructor | |
| has |
Note that, Array method includes and indexOf are internationally different and use different algorithm to detect NaN array elements. Also, any array element which is missing is treated as undefined.
ToInt32 ( argument )
ToInt32 is an abstract operation and takes argument argument. The argument is converted to one of 232 integral Number values ranging from 𝔽(-231) to 𝔽(231 - 1), The algorithm or rules it follows are listed below:
- Let number be ? ToNumber(argument).
- If number is NaN, +0𝔽, -0𝔽, +∞𝔽, or -∞𝔽, return +0𝔽.
- Let int be the mathematical value whose sign is the sign of number and whose magnitude is floor(abs(ℝ(number))).
- Let int32bit be int modulo 232.
- If int32bit ≥ 231, return 𝔽(int32bit - 232); otherwise return 𝔽(int32bit).
- The ToInt32 is an abstract operation that can be applied multiple times without changing the result i.e. it is idempotent.
- Since, +∞𝔽 and -∞𝔽 are mapped to +0𝔽 the operation ToInt32(ToUint32(x)) = ToInt32(x)
Reference: ECMAScript Language Specification
Mostly, majority of other function use ToInt32 when operating on numbers.
Hope this article gave insight to few of the JavaScript abstract operations.
![]() | ![]() | ![]() | ![]() |
| Like | Comment | Save | Share |




/api/attachments/att/4/linkedin-null-nan-1200-628.png/linkedin-null-nan-1200-628.100.jpg)
/api/attachments/att/249/webP.gif/webP.100.jpg)