These operations are not a part of the ECMAScript language; they are defined here solely to aid the specification of the semantics of the ECMAScript language. Other, more specialized abstract operations are defined throughout this specification.
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 abstract operations. The conversion abstract operations are polymorphic; they can accept a value of any ECMAScript language type. But no other specification types are used with these operations.
The BigInt type has no implicit conversions in the ECMAScript language; programmers must call BigInt explicitly to convert values from other types.
7.1.1 ToPrimitive ( input [ , preferredType ] )
The abstract operation ToPrimitive takes argument input and optional argument preferredType. It converts its input argument to a non-Object type. If an object is capable of converting to more than one primitive type, it may use the optional hint preferredType to favour that type. It performs the following steps when called:
When ToPrimitive is called with no hint, then it generally behaves as if the hint were number. However, objects may over-ride this behaviour by defining a @@toPrimitive method. Of the objects defined in this specification only Date objects (see 21.4.4.45) and Symbol objects (see 20.4.3.5) over-ride the default ToPrimitive behaviour. Date objects treat no hint as if the hint were string.
7.1.1.1 OrdinaryToPrimitive ( O, hint )
The abstract operation OrdinaryToPrimitive takes arguments O and hint. It performs the following steps when called:
The abstract operation ToNumeric takes argument value. It returns value converted to a Number or a BigInt. It performs the following steps when called:
ToNumber applied to Strings applies the following grammar to the input String interpreted as a sequence of UTF-16 encoded code points (6.1.4). If the grammar cannot interpret the String as an expansion of StringNumericLiteral, then the result of ToNumber is NaN.
Note 1
The terminal symbols of this grammar are all composed of characters in the Unicode Basic Multilingual Plane (BMP). Therefore, the result of ToNumber will be NaN if the string contains any leading surrogate or trailing surrogate code units, whether paired or unpaired.
The conversion of a String to a Number value is similar overall to the determination of the Number value for a numeric literal (see 12.8.3), but some of the details are different, so the process for converting a String numeric literal to a value of Number type is given here. This value is determined in two steps: first, a mathematical value (MV) is derived from the String numeric literal; second, this mathematical value is rounded as described below. The MV on any grammar symbol, not provided below, is the MV for that symbol defined in 12.8.3.1.
Once the exact MV for a String numeric literal has been determined, it is then rounded to a value of the Number type. If the MV is 0, then the rounded value is +0𝔽 unless the first non white space code point in the String numeric literal is -, in which case the rounded value is -0𝔽. Otherwise, the rounded value must be the Number value for the MV (in the sense defined in 6.1.6.1), unless the literal includes a StrUnsignedDecimalLiteral and the literal has more than 20 significant digits, in which case the Number value may be either the Number value for the MV of a literal produced by replacing each significant digit after the 20th with a 0 digit or the Number value for the MV of a literal produced by replacing each significant digit after the 20th with a 0 digit and then incrementing the literal at the 20th digit position. A digit is significant if it is not part of an ExponentPart and
it is not 0; or
there is a non-zero digit to its left and there is a non-zero digit, not in the ExponentPart, to its right.
7.1.5 ToIntegerOrInfinity ( argument )
The abstract operation ToIntegerOrInfinity takes argument argument. It converts argument to an integer, +∞, or -∞. It performs the following steps when called:
The abstract operation ToInt32 takes argument argument. It converts argument to one of 232integral Number values in the range 𝔽(-231) through 𝔽(231 - 1), inclusive. It performs the following steps when called:
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. It converts argument to one of 232integral Number values in the range +0𝔽 through 𝔽(232 - 1), inclusive. It performs the following steps when called:
Step 5 is the only difference between ToUint32 and ToInt32.
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. It converts argument to one of 216integral Number values in the range 𝔽(-215) through 𝔽(215 - 1), inclusive. It performs the following steps when called:
The abstract operation ToUint16 takes argument argument. It converts argument to one of 216integral Number values in the range +0𝔽 through 𝔽(216 - 1), inclusive. It performs the following steps when called:
The substitution of 216 for 232 in step 4 is the only difference between ToUint32 and ToUint16.
ToUint16 maps -0𝔽 to +0𝔽.
7.1.10 ToInt8 ( argument )
The abstract operation ToInt8 takes argument argument. It converts argument to one of 28integral Number values in the range -128𝔽 through 127𝔽, inclusive. It performs the following steps when called:
The abstract operation ToUint8 takes argument argument. It converts argument to one of 28integral Number values in the range +0𝔽 through 255𝔽, inclusive. It performs the following steps when called:
The abstract operation ToUint8Clamp takes argument argument. It converts argument to one of 28integral Number values in the range +0𝔽 through 255𝔽, inclusive. It performs the following steps when called:
Unlike the other ECMAScript integer conversion abstract operation, ToUint8Clamp rounds rather than truncates non-integral values and does not convert +∞𝔽 to +0𝔽. ToUint8Clamp does “round half to even” tie-breaking. This differs from Math.round which does “round half up” tie-breaking.
7.1.13 ToBigInt ( argument )
The abstract operation ToBigInt takes argument argument. It converts argument to a BigInt value, or throws if an implicit conversion from Number would be required. It performs the following steps when called:
If the MV is NaN, return NaN, otherwise return the BigInt which exactly corresponds to the MV, rather than rounding to a Number.
7.1.15 ToBigInt64 ( argument )
The abstract operation ToBigInt64 takes argument argument. It converts argument to one of 264 BigInt values in the range ℤ(-263) through ℤ(263-1), inclusive. It performs the following steps when called:
The abstract operation ToBigUint64 takes argument argument. It converts argument to one of 264 BigInt values in the range 0ℤ through the BigInt value for ℤ(264-1), inclusive. It performs the following steps when called:
Return a new Boolean object whose [[BooleanData]] internal slot is set to argument. See 20.3 for a description of Boolean objects.
Number
Return a new Number object whose [[NumberData]] internal slot is set to argument. See 21.1 for a description of Number objects.
String
Return a new String object whose [[StringData]] internal slot is set to argument. See 22.1 for a description of String objects.
Symbol
Return a new Symbol object whose [[SymbolData]] internal slot is set to argument. See 20.4 for a description of Symbol objects.
BigInt
Return a new BigInt object whose [[BigIntData]] internal slot is set to argument. See 21.2 for a description of BigInt objects.
Object
Return argument.
7.1.19 ToPropertyKey ( argument )
The abstract operation ToPropertyKey takes argument argument. It converts argument to a value that can be used as a property key. It performs the following steps when called:
The abstract operation ToLength takes argument argument. It converts argument to an integral Number suitable for use as the length of an array-like object. It performs the following steps when called:
The abstract operation CanonicalNumericIndexString takes argument argument. It returns argument converted to a Number value if it is a String representation of a Number that would be produced by ToString, or the string "-0". Otherwise, it returns undefined. It performs the following steps when called:
A canonical numeric string is any String value for which the CanonicalNumericIndexString abstract operation does not return undefined.
7.1.22 ToIndex ( value )
The abstract operation ToIndex takes argument value. It returns value argument converted to a non-negative integer if it is a valid integer index value. It performs the following steps when called:
The abstract operation RequireObjectCoercible takes argument argument. It throws an error if argument is a value that cannot be converted to an Object using ToObject. It is defined by Table 16:
The abstract operation IsCallable takes argument argument (an ECMAScript language value). It determines if argument is a callable function with a [[Call]] internal method. It performs the following steps when called:
If argument has a [[Call]] internal method, return true.
Return false.
7.2.4 IsConstructor ( argument )
The abstract operation IsConstructor takes argument argument (an ECMAScript language value). It determines if argument is a function object with a [[Construct]] internal method. It performs the following steps when called:
If argument has a [[Construct]] internal method, return true.
Return false.
7.2.5 IsExtensible ( O )
The abstract operation IsExtensible takes argument O (an Object) and returns a completion record which, if its [[Type]] is normal, has a [[Value]] which is a Boolean. It is used to determine whether additional properties can be added to O. It performs the following steps when called:
The abstract operation IsIntegralNumber takes argument argument. It determines if argument is a finite integral Number value. It performs the following steps when called:
If floor(abs(ℝ(argument))) ≠ abs(ℝ(argument)), return false.
Return true.
7.2.7 IsPropertyKey ( argument )
The abstract operation IsPropertyKey takes argument argument (an ECMAScript language value). It determines if argument is a value that may be used as a property key. It performs the following steps when called:
If matcher is not undefined, return ! ToBoolean(matcher).
If argument has a [[RegExpMatcher]] internal slot, return true.
Return false.
7.2.9 IsStringPrefix ( p, q )
The abstract operation IsStringPrefix takes arguments p (a String) and q (a String). It determines if p is a prefix of q. It performs the following steps when called:
If q can be the string-concatenation of p and some other String r, return true. Otherwise, return false.
Note
Any String is a prefix of itself, because r may be the empty String.
7.2.10 SameValue ( x, y )
The abstract operation SameValue takes arguments x (an ECMAScript language value) and y (an ECMAScript language value) and returns a completion record whose [[Type]] is normal and whose [[Value]] is a Boolean. It performs the following steps when called:
If Type(x) is different from Type(y), return false.
This algorithm differs from the Strict Equality Comparison Algorithm in its treatment of signed zeroes and NaNs.
7.2.11 SameValueZero ( x, y )
The abstract operation SameValueZero takes arguments x (an ECMAScript language value) and y (an ECMAScript language value) and returns a completion record whose [[Type]] is normal and whose [[Value]] is a Boolean. It performs the following steps when called:
If Type(x) is different from Type(y), return false.
SameValueZero differs from SameValue only in its treatment of +0𝔽 and -0𝔽.
7.2.12 SameValueNonNumeric ( x, y )
The abstract operation SameValueNonNumeric takes arguments x (an ECMAScript language value) and y (an ECMAScript language value) and returns a completion record whose [[Type]] is normal and whose [[Value]] is a Boolean. It performs the following steps when called:
If x and y are exactly the same sequence of code units (same length and same code units at corresponding indices), return true; otherwise, return false.
If x and y are both the same Symbol value, return true; otherwise, return false.
If x and y are the same Object value, return true. Otherwise, return false.
7.2.13 Abstract Relational Comparison
The comparison x < y, where x and y are values, produces true, false, or undefined (which indicates that at least one operand is NaN). In addition to x and y the algorithm takes a Boolean flag named LeftFirst as a parameter. The flag is used to control the order in which operations with potentially visible side-effects are performed upon x and y. It is necessary because ECMAScript specifies left to right evaluation of expressions. The default value of LeftFirst is true and indicates that the x parameter corresponds to an expression that occurs to the left of the y parameter's corresponding expression. If LeftFirst is false, the reverse is the case and operations must be performed upon y before x. Such a comparison is performed as follows:
Let k be the smallest non-negative integer such that the code unit at index k within px is different from the code unit at index k within py. (There must be such a k, for neither String is a prefix of the other.)
Let m be the integer that is the numeric value of the code unit at index k within px.
Let n be the integer that is the numeric value of the code unit at index k within py.
If m < n, return true. Otherwise, return false.
Else,
If Type(px) is BigInt and Type(py) is String, then
If Type(nx) is the same as Type(ny), return Type(nx)::lessThan(nx, ny).
Assert: Type(nx) is BigInt and Type(ny) is Number, or Type(nx) is Number and Type(ny) is BigInt.
If nx or ny is NaN, return undefined.
If nx is -∞𝔽 or ny is +∞𝔽, return true.
If nx is +∞𝔽 or ny is -∞𝔽, return false.
If ℝ(nx) < ℝ(ny), return true; otherwise return false.
Note 1
Step 3 differs from step 2.c in the algorithm that handles the addition operator + (13.15.3) by using the logical-and operation instead of the logical-or operation.
Note 2
The comparison of Strings uses a simple lexicographic ordering on sequences of code unit values. There is no attempt to use the more complex, semantically oriented definitions of character or string equality and collating order defined in the Unicode specification. Therefore String values that are canonically equal according to the Unicode standard could test as unequal. In effect this algorithm assumes that both Strings are already in normalized form. Also, note that for strings containing supplementary characters, lexicographic ordering on sequences of UTF-16 code unit values differs from that on sequences of code point values.
7.2.14 Abstract Equality Comparison
The comparison x == y, where x and y are values, produces true or false. Such a comparison is performed as follows:
This algorithm differs from the SameValue Algorithm in its treatment of signed zeroes and NaNs.
7.3 Operations on Objects
7.3.1 MakeBasicObject ( internalSlotsList )
The abstract operation MakeBasicObject takes argument internalSlotsList. It is the source of all ECMAScript objects that are created algorithmically, including both ordinary objects and exotic objects. It factors out common steps used in creating all objects, and centralizes object creation. It performs the following steps when called:
Assert: internalSlotsList is a List of internal slot names.
Let obj be a newly created object with an internal slot for each name in internalSlotsList.
Set obj's essential internal methods to the default ordinary object definitions specified in 10.1.
Assert: If the caller will not be overriding both obj's [[GetPrototypeOf]] and [[SetPrototypeOf]] essential internal methods, then internalSlotsList contains [[Prototype]].
Assert: If the caller will not be overriding all of obj's [[SetPrototypeOf]], [[IsExtensible]], and [[PreventExtensions]] essential internal methods, then internalSlotsList contains [[Extensible]].
If internalSlotsList contains [[Extensible]], set obj.[[Extensible]] to true.
Return obj.
Note
Within this specification, exotic objects are created in abstract operations such as ArrayCreate and BoundFunctionCreate by first calling MakeBasicObject to obtain a basic, foundational object, and then overriding some or all of that object's internal methods. In order to encapsulate exotic object creation, the object's essential internal methods are never modified outside those operations.
7.3.2 Get ( O, P )
The abstract operation Get takes arguments O (an Object) and P (a property key). It is used to retrieve the value of a specific property of an object. It performs the following steps when called:
The abstract operation GetV takes arguments V (an ECMAScript language value) and P (a property key). It is used to retrieve the value of a specific property of an ECMAScript language value. If the value is not an object, the property lookup is performed using a wrapper object appropriate for the type of the value. It performs the following steps when called:
The abstract operation Set takes arguments O (an Object), P (a property key), V (an ECMAScript language value), and Throw (a Boolean). It is used to set the value of a specific property of an object. V is the new value for the property. It performs the following steps when called:
If success is false and Throw is true, throw a TypeError exception.
Return success.
7.3.5 CreateDataProperty ( O, P, V )
The abstract operation CreateDataProperty takes arguments O (an Object), P (a property key), and V (an ECMAScript language value). It is used to create a new own property of an object. It performs the following steps when called:
Let newDesc be the PropertyDescriptor { [[Value]]: V, [[Writable]]: true, [[Enumerable]]: true, [[Configurable]]: true }.
Return ? O.[[DefineOwnProperty]](P, newDesc).
Note
This abstract operation creates a property whose attributes are set to the same defaults used for properties created by the ECMAScript language assignment operator. Normally, the property will not already exist. If it does exist and is not configurable or if O is not extensible, [[DefineOwnProperty]] will return false.
7.3.6 CreateMethodProperty ( O, P, V )
The abstract operation CreateMethodProperty takes arguments O (an Object), P (a property key), and V (an ECMAScript language value). It is used to create a new own property of an object. It performs the following steps when called:
Let newDesc be the PropertyDescriptor { [[Value]]: V, [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: true }.
Return ? O.[[DefineOwnProperty]](P, newDesc).
Note
This abstract operation creates a property whose attributes are set to the same defaults used for built-in methods and methods defined using class declaration syntax. Normally, the property will not already exist. If it does exist and is not configurable or if O is not extensible, [[DefineOwnProperty]] will return false.
7.3.7 CreateDataPropertyOrThrow ( O, P, V )
The abstract operation CreateDataPropertyOrThrow takes arguments O (an Object), P (a property key), and V (an ECMAScript language value). It is used to create a new own property of an object. It throws a TypeError exception if the requested property update cannot be performed. It performs the following steps when called:
This abstract operation creates a property whose attributes are set to the same defaults used for properties created by the ECMAScript language assignment operator. Normally, the property will not already exist. If it does exist and is not configurable or if O is not extensible, [[DefineOwnProperty]] will return false causing this operation to throw a TypeError exception.
7.3.8 DefinePropertyOrThrow ( O, P, desc )
The abstract operation DefinePropertyOrThrow takes arguments O (an Object), P (a property key), and desc (a Property Descriptor). It is used to call the [[DefineOwnProperty]] internal method of an object in a manner that will throw a TypeError exception if the requested property update cannot be performed. It performs the following steps when called:
Let success be ? O.[[DefineOwnProperty]](P, desc).
If success is false, throw a TypeError exception.
Return success.
7.3.9 DeletePropertyOrThrow ( O, P )
The abstract operation DeletePropertyOrThrow takes arguments O (an Object) and P (a property key). It is used to remove a specific own property of an object. It throws an exception if the property is not configurable. It performs the following steps when called:
The abstract operation GetMethod takes arguments V (an ECMAScript language value) and P (a property key). It is used to get the value of a specific property of an ECMAScript language value when the value of the property is expected to be a function. It performs the following steps when called:
If func is either undefined or null, return undefined.
If IsCallable(func) is false, throw a TypeError exception.
Return func.
7.3.11 HasProperty ( O, P )
The abstract operation HasProperty takes arguments O (an Object) and P (a property key) and returns a completion record which, if its [[Type]] is normal, has a [[Value]] which is a Boolean. It is used to determine whether an object has a property with the specified property key. The property may be either an own or inherited. It performs the following steps when called:
The abstract operation HasOwnProperty takes arguments O (an Object) and P (a property key) and returns a completion record which, if its [[Type]] is normal, has a [[Value]] which is a Boolean. It is used to determine whether an object has an own property with the specified property key. It performs the following steps when called:
The abstract operation Call takes arguments F (an ECMAScript language value) and V (an ECMAScript language value) and optional argument argumentsList (a List of ECMAScript language values). It is used to call the [[Call]] internal method of a function object. F is the function object, V is an ECMAScript language value that is the this value of the [[Call]], and argumentsList is the value passed to the corresponding argument of the internal method. If argumentsList is not present, a new empty List is used as its value. It performs the following steps when called:
If argumentsList is not present, set argumentsList to a new empty List.
If IsCallable(F) is false, throw a TypeError exception.
The abstract operation Construct takes argument F (a function object) and optional arguments argumentsList and newTarget. It is used to call the [[Construct]] internal method of a function object. argumentsList and newTarget are the values to be passed as the corresponding arguments of the internal method. If argumentsList is not present, a new empty List is used as its value. If newTarget is not present, F is used as its value. It performs the following steps when called:
If newTarget is not present, set newTarget to F.
If argumentsList is not present, set argumentsList to a new empty List.
If newTarget is not present, this operation is equivalent to: new F(...argumentsList)
7.3.15 SetIntegrityLevel ( O, level )
The abstract operation SetIntegrityLevel takes arguments O and level. It is used to fix the set of own properties of an object. It performs the following steps when called:
The abstract operation TestIntegrityLevel takes arguments O and level. It is used to determine if the set of own properties of an object are fixed. It performs the following steps when called:
NOTE: If the object is extensible, none of its properties are examined.
Let keys be ? O.[[OwnPropertyKeys]]().
For each element k of keys, do
Let currentDesc be ? O.[[GetOwnProperty]](k).
If currentDesc is not undefined, then
If currentDesc.[[Configurable]] is true, return false.
If level is frozen and IsDataDescriptor(currentDesc) is true, then
If currentDesc.[[Writable]] is true, return false.
Return true.
7.3.17 CreateArrayFromList ( elements )
The abstract operation CreateArrayFromList takes argument elements (a List). It is used to create an Array object whose elements are provided by elements. It performs the following steps when called:
Assert: elements is a List whose elements are all ECMAScript language values.
The abstract operation LengthOfArrayLike takes argument obj. It returns the value of the "length" property of an array-like object (as a non-negative integer). It performs the following steps when called:
The abstract operation CreateListFromArrayLike takes argument obj and optional argument elementTypes (a List of names of ECMAScript Language Types). It is used to create a List value whose elements are provided by the indexed properties of obj. elementTypes contains the names of ECMAScript Language Types that are allowed for element values of the List that is created. It performs the following steps when called:
If elementTypes is not present, set elementTypes to « Undefined, Null, Boolean, String, Symbol, Number, BigInt, Object ».
If Type(obj) is not Object, throw a TypeError exception.
If Type(next) is not an element of elementTypes, throw a TypeError exception.
Append next as the last element of list.
Set index to index + 1.
Return list.
7.3.20 Invoke ( V, P [ , argumentsList ] )
The abstract operation Invoke takes arguments V (an ECMAScript language value) and P (a property key) and optional argument argumentsList (a List of ECMAScript language values). It is used to call a method property of an ECMAScript language value. V serves as both the lookup point for the property and the this value of the call. argumentsList is the list of arguments values passed to the method. If argumentsList is not present, a new empty List is used as its value. It performs the following steps when called:
The abstract operation OrdinaryHasInstance takes arguments C (an ECMAScript language value) and O. It implements the default algorithm for determining if O inherits from the instance object inheritance path provided by C. It performs the following steps when called:
7.3.22 SpeciesConstructor ( O, defaultConstructor )
The abstract operation SpeciesConstructor takes arguments O (an Object) and defaultConstructor (a constructor). It is used to retrieve the constructor that should be used to create new objects that are derived from O. defaultConstructor is the constructor to use if a constructor@@species property cannot be found starting from O. It performs the following steps when called:
The abstract operation EnumerableOwnPropertyNames takes arguments O (an Object) and kind (one of key, value, or key+value). It performs the following steps when called:
The abstract operation IteratorStep takes argument iteratorRecord. It requests the next value from iteratorRecord.[[Iterator]] by calling iteratorRecord.[[NextMethod]] and returns either false indicating that the iterator has reached its end or the IteratorResult object if a next value is available. It performs the following steps when called:
The abstract operation IteratorClose takes arguments iteratorRecord and completion. It is used to notify an iterator that it should perform any actions it would normally perform when it has reached its completed state. It performs the following steps when called:
Assert: Type(iteratorRecord.[[Iterator]]) is Object.
The abstract operation AsyncIteratorClose takes arguments iteratorRecord and completion. It is used to notify an async iterator that it should perform any actions it would normally perform when it has reached its completed state. It performs the following steps when called:
Assert: Type(iteratorRecord.[[Iterator]]) is Object.
The abstract operation CreateIterResultObject takes arguments value and done. It creates an object that supports the IteratorResult interface. It performs the following steps when called:
The abstract operation CreateListIteratorRecord takes argument list. It creates an Iterator (27.1.1.2) object record whose next method returns the successive elements of list. It performs the following steps when called:
Let closure be a new Abstract Closure with no parameters that captures list and performs the following steps when called: