BY Simmi Kava28 Apr 2021 Edit
JavaScript Numbers: ∞ -> 0 | ∞ -> maxValue

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:

  1. Let number be ? ToNumber(argument).
  2. If number is NaN, +0𝔽, or -0𝔽, return 0.
  3. If number is +∞𝔽, return +∞.
  4. If number is -∞𝔽, return -∞.
  5. Let integer be floor(abs(ℝ(number))).
  6. If number < +0𝔽, set integer to -integer.
  7. 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:

  1. If Type(x) is different from Type(y), return false.
  2. If Type(x) is Number or BigInt, then a. Return ! Type(x)::sameValueZero(x, y).
  3. Return ! SameValueNonNumeric(x, y).
  • SameValueZero differs from SameValue only 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:

  1. Let number be ? ToNumber(argument).
  2. If number is NaN, +0𝔽, -0𝔽, +∞𝔽, or -∞𝔽, return +0𝔽.
  3. Let int be the mathematical value whose sign is the sign of number and whose magnitude is floor(abs(ℝ(number))).
  4. Let int32bit be int modulo 232.
  5. 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.

BY Simmi Kava
LikeCommentSave
LikeCommentSaveShare
0
Categories
General
YantraJS
Developer Guides
Tutorials
Web Atoms Updates

POPULAR POSTS
17 Mar 2021
LATEST ACTIVITY
Simmi Kava
commented this post.
Simmi Kava
liked this post.
Show more
ARCHIVES
2025
2024
2023
2022
TAGS
javascript (56)
developer (25)
javascriptdeveloper (16)
Xamarin.Forms (16)
Html (14)
typescript (12)
webatoms (12)
xamarin (11)
coding (10)
web-atoms (10)
arrays (9)
android (8)
javascript-developer (8)
csharp (7)
dotnet (7)
css (6)
update (6)
dotnet-standard (5)
function (5)
iOS (5)
methods (4)




Web Atoms: JSX (TSX + TypeScript) for Xamarin.Forms, Hot Reload Your App in Production Environment

PlaygroundSamples Repository