7.1 Type Conversion
The ECMAScript language implicitly performs automatic type conversion as needed. To clarify the semantics of certain constructs it is useful to define a set of conversion
The
7.1.1 ToPrimitive ( input [ , preferredType ] )
The abstract operation ToPrimitive takes argument input (an
- If input
is an Object , then- Let exoticToPrim be ?
GetMethod (input,@@toPrimitive ). - If exoticToPrim is not
undefined , then- If preferredType is not present, then
- Let hint be
"default" .
- Let hint be
- Else if preferredType is
string , then- Let hint be
"string" .
- Let hint be
- Else,
Assert : preferredType isnumber .- Let hint be
"number" .
- Let result be ?
Call (exoticToPrim, input, « hint »). - If result
is not an Object , return result. - Throw a
TypeError exception.
- If preferredType is not present, then
- If preferredType is not present, let preferredType be
number . - Return ?
OrdinaryToPrimitive (input, preferredType).
- Let exoticToPrim be ?
- Return input.
When ToPrimitive is called without a hint, then it generally behaves as if the hint were
7.1.1.1 OrdinaryToPrimitive ( O, hint )
The abstract operation OrdinaryToPrimitive takes arguments O (an Object) and hint (
- If hint is
string , then- Let methodNames be «
"toString" ,"valueOf" ».
- Let methodNames be «
- Else,
- Let methodNames be «
"valueOf" ,"toString" ».
- Let methodNames be «
- For each element name of methodNames, do
- Let method be ?
Get (O, name). - If
IsCallable (method) istrue , then- Let result be ?
Call (method, O). - If result
is not an Object , return result.
- Let result be ?
- Let method be ?
- Throw a
TypeError exception.
7.1.2 ToBoolean ( argument )
The abstract operation ToBoolean takes argument argument (an
- If argument
is a Boolean , return argument. - If argument is one of
undefined ,null ,+0 𝔽,-0 𝔽,NaN ,0 ℤ, or the empty String, returnfalse . - NOTE: This step is replaced in section
B.3.6.1 . - Return
true .
7.1.3 ToNumeric ( value )
The abstract operation ToNumeric takes argument value (an
- Let primValue be ?
ToPrimitive (value,number ). - If primValue
is a BigInt , return primValue. - Return ?
ToNumber (primValue).
7.1.4 ToNumber ( argument )
The abstract operation ToNumber takes argument argument (an
- If argument
is a Number , return argument. - If argument is either a Symbol or a BigInt, throw a
TypeError exception. - If argument is
undefined , returnNaN . - If argument is either
null orfalse , return+0 𝔽. - If argument is
true , return1 𝔽. - If argument
is a String , returnStringToNumber (argument). Assert : argumentis an Object .- Let primValue be ?
ToPrimitive (argument,number ). Assert : primValueis not an Object .- Return ?
ToNumber (primValue).
7.1.4.1 ToNumber Applied to the String Type
The abstract operation
Syntax
All grammar symbols not explicitly defined above have the definitions used in the Lexical Grammar for numeric literals (
Some differences should be noted between the syntax of a
-
A
StringNumericLiteral may include leading and/or trailing white space and/or line terminators. -
A
StringNumericLiteral that is decimal may have any number of leading0
digits. -
A
StringNumericLiteral that is decimal may include a+
or-
to indicate its sign. -
A
StringNumericLiteral that is empty or contains only white space is converted to+0 𝔽. -
Infinity
and-Infinity
are recognized as aStringNumericLiteral but not as aNumericLiteral . -
A
StringNumericLiteral cannot include aBigIntLiteralSuffix . -
A
StringNumericLiteral cannot include aNumericLiteralSeparator .
7.1.4.1.1 StringToNumber ( str )
The abstract operation StringToNumber takes argument str (a String) and returns a Number. It performs the following steps when called:
- Let text be
StringToCodePoints (str). - Let literal be
ParseText (text,StringNumericLiteral ). - If literal is a
List of errors, returnNaN . - Return
StringNumericValue of literal.
7.1.4.1.2 Runtime Semantics: StringNumericValue
The
The conversion of a
It is defined piecewise over the following productions:
- Return
+0 𝔽.
- Return
StringNumericValue ofStrNumericLiteral .
- Return
𝔽 (MV ofNonDecimalIntegerLiteral ).
- Let a be
StringNumericValue ofStrUnsignedDecimalLiteral . - If a is
+0 𝔽, return-0 𝔽. - Return -a.
- Return
+∞ 𝔽.
- Let a be MV of the first
DecimalDigits . - If the second
DecimalDigits is present, then- Let b be MV of the second
DecimalDigits . - Let n be the number of code points in the second
DecimalDigits .
- Let b be MV of the second
- Else,
- Let b be 0.
- Let n be 0.
- If
ExponentPart is present, let e be MV ofExponentPart . Otherwise, let e be 0. - Return
RoundMVResult ((a + (b × 10-n)) × 10e).
- Let b be MV of
DecimalDigits . - If
ExponentPart is present, let e be MV ofExponentPart . Otherwise, let e be 0. - Let n be the number of code points in
DecimalDigits . - Return
RoundMVResult (b × 10e - n).
- Let a be MV of
DecimalDigits . - If
ExponentPart is present, let e be MV ofExponentPart . Otherwise, let e be 0. - Return
RoundMVResult (a × 10e).
7.1.4.1.3 RoundMVResult ( n )
The abstract operation RoundMVResult takes argument n (a
- If the decimal representation of n has 20 or fewer significant digits, return
𝔽 (n). - Let option1 be the
mathematical value denoted by the result of replacing each significant digit in the decimal representation of n after the 20th with a 0 digit. - Let option2 be the
mathematical value denoted by the result of replacing each significant digit in the decimal representation of n after the 20th with a 0 digit and then incrementing it at the 20th position (with carrying as necessary). - Let chosen be an
implementation-defined choice of either option1 or option2. - Return
𝔽 (chosen).
7.1.5 ToIntegerOrInfinity ( argument )
The abstract operation ToIntegerOrInfinity takes argument argument (an
7.1.6 ToInt32 ( argument )
The abstract operation ToInt32 takes argument argument (an
Given the above definition of ToInt32:
- The ToInt32 abstract operation is idempotent: if applied to a result that it produced, the second application leaves that value unchanged.
-
ToInt32(
ToUint32 (x)) is the same value as ToInt32(x) for all values of x. (It is to preserve this latter property that+∞ 𝔽 and-∞ 𝔽 are mapped to+0 𝔽.) -
ToInt32 maps
-0 𝔽 to+0 𝔽.
7.1.7 ToUint32 ( argument )
The abstract operation ToUint32 takes argument argument (an
Given the above definition of ToUint32:
-
Step
5 is the only difference between ToUint32 andToInt32 . - The ToUint32 abstract operation is idempotent: if applied to a result that it produced, the second application leaves that value unchanged.
-
ToUint32(
ToInt32 (x)) is the same value as ToUint32(x) for all values of x. (It is to preserve this latter property that+∞ 𝔽 and-∞ 𝔽 are mapped to+0 𝔽.) -
ToUint32 maps
-0 𝔽 to+0 𝔽.
7.1.8 ToInt16 ( argument )
The abstract operation ToInt16 takes argument argument (an
7.1.9 ToUint16 ( argument )
The abstract operation ToUint16 takes argument argument (an
7.1.10 ToInt8 ( argument )
The abstract operation ToInt8 takes argument argument (an
7.1.11 ToUint8 ( argument )
The abstract operation ToUint8 takes argument argument (an
7.1.12 ToUint8Clamp ( argument )
The abstract operation ToUint8Clamp takes argument argument (an
- Let number be ?
ToNumber (argument). - If number is
NaN , return+0 𝔽. - Let mv be the
extended mathematical value of number. - Let clamped be the result of
clamping mv between 0 and 255. - Let f be
floor (clamped). - If clamped < f + 0.5, return
𝔽 (f). - If clamped > f + 0.5, return
𝔽 (f + 1). - If f is even, return
𝔽 (f). Otherwise, return𝔽 (f + 1).
Unlike most other ECMAScript Math.round
7.1.13 ToBigInt ( argument )
The abstract operation ToBigInt takes argument argument (an
- Let prim be ?
ToPrimitive (argument,number ). - Return the value that prim corresponds to in
Table 12 .
Argument Type | Result |
---|---|
Undefined |
Throw a |
Null |
Throw a |
Boolean |
Return 1n if prim is 0n if prim is |
BigInt | Return prim. |
Number |
Throw a |
String |
|
Symbol |
Throw a |
7.1.14 StringToBigInt ( str )
The abstract operation StringToBigInt takes argument str (a String) and returns a BigInt or
- Let text be
StringToCodePoints (str). - Let literal be
ParseText (text,StringIntegerLiteral ). - If literal is a
List of errors, returnundefined . - Let mv be the MV of literal.
Assert : mv is aninteger .- Return
ℤ (mv).
7.1.14.1 StringIntegerLiteral Grammar
Syntax
7.1.14.2 Runtime Semantics: MV
-
The MV of
is 0.StringIntegerLiteral ::: StrWhiteSpace opt -
The MV of
is the MV ofStringIntegerLiteral ::: StrWhiteSpace opt StrIntegerLiteral StrWhiteSpace opt StrIntegerLiteral .
7.1.15 ToBigInt64 ( argument )
The abstract operation ToBigInt64 takes argument argument (an
7.1.16 ToBigUint64 ( argument )
The abstract operation ToBigUint64 takes argument argument (an
7.1.17 ToString ( argument )
The abstract operation ToString takes argument argument (an
- If argument
is a String , return argument. - If argument
is a Symbol , throw aTypeError exception. - If argument is
undefined , return"undefined" . - If argument is
null , return"null" . - If argument is
true , return"true" . - If argument is
false , return"false" . - If argument
is a Number , returnNumber::toString (argument, 10). - If argument
is a BigInt , returnBigInt::toString (argument, 10). Assert : argumentis an Object .- Let primValue be ?
ToPrimitive (argument,string ). Assert : primValueis not an Object .- Return ?
ToString (primValue).
7.1.18 ToObject ( argument )
The abstract operation ToObject takes argument argument (an
Argument Type | Result |
---|---|
Undefined |
Throw a |
Null |
Throw a |
Boolean |
Return a new Boolean object whose [[BooleanData]] internal slot is set to argument. See |
Number |
Return a new Number object whose [[NumberData]] internal slot is set to argument. See |
String |
Return a new String object whose [[StringData]] internal slot is set to argument. See |
Symbol |
Return a new Symbol object whose [[SymbolData]] internal slot is set to argument. See |
BigInt |
Return a new BigInt object whose [[BigIntData]] internal slot is set to argument. See |
Object | Return argument. |
7.1.19 ToPropertyKey ( argument )
The abstract operation ToPropertyKey takes argument argument (an
- Let key be ?
ToPrimitive (argument,string ). - If key
is a Symbol , then- Return key.
- Return !
ToString (key).
7.1.20 ToLength ( argument )
The abstract operation ToLength takes argument argument (an
- Let len be ?
ToIntegerOrInfinity (argument). - If len ≤ 0, return
+0 𝔽. - Return
𝔽 (min (len, 253 - 1)).
7.1.21 CanonicalNumericIndexString ( argument )
The abstract operation CanonicalNumericIndexString takes argument argument (a String) and returns a Number or
A canonical numeric string is any String value for which the CanonicalNumericIndexString abstract operation does not return
7.1.22 ToIndex ( value )
The abstract operation ToIndex takes argument value (an
- Let integer be ?
ToIntegerOrInfinity (value). - If integer is not in the
inclusive interval from 0 to 253 - 1, throw aRangeError exception. - Return integer.