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 specializedabstract 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 conversionabstract operations. The conversionabstract operations are polymorphic; they can accept a value of anyECMAScript language type. But no other specification types are used with these operations.
TheBigInt 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 argumentinput (anECMAScript language value) and optional argumentpreferredType (string ornumber) and returns either anormal completion containing anECMAScript language value or athrow completion. It converts itsinput argument to a non-Object type. If an object is capable of converting to more than one primitive type, it may use the optional hintpreferredType to favour that type. It performs the following steps when called:
When ToPrimitive is called without a hint, then it generally behaves as if the hint werenumber. However, objects may over-ride this behaviour by defining a%Symbol.toPrimitive% method. Of the objects defined in this specification only Dates (see21.4.4.45) and Symbol objects (see20.4.3.5) over-ride the default ToPrimitive behaviour. Dates treat the absence of a hint as if the hint werestring.
The abstract operation ToBoolean takes argumentargument (anECMAScript language value) and returns a Boolean. It convertsargument to a value of type Boolean. It performs the following steps when called:
The abstract operation ToNumeric takes argumentvalue (anECMAScript language value) and returns either anormal completion containing either a Number or a BigInt, or athrow completion. It returnsvalue converted to a Number or a BigInt. It performs the following steps when called:
The abstract operation RoundMVResult takes argumentn (amathematical value) and returns a Number. It convertsn to a Number in animplementation-defined manner. For the purposes of this abstract operation, a digit is significant if it is not zero or there is a non-zero digit to its left and there is a non-zero digit to its right. For the purposes of this abstract operation, "themathematical value denoted by" a representation of amathematical value is the inverse of "the decimal representation of" amathematical value. It performs the following steps when called:
If the decimal representation ofn has 20 or fewer significant digits, return𝔽(n).
Letoption1 be themathematical value denoted by the result of replacing each significant digit in the decimal representation ofn after the 20th with a 0 digit.
Letoption2 be themathematical value denoted by the result of replacing each significant digit in the decimal representation ofn after the 20th with a 0 digit and then incrementing it at the 20th position (with carrying as necessary).
The abstract operation ToIntegerOrInfinity takes argumentargument (anECMAScript language value) and returns either anormal completion containing either aninteger, +∞, or -∞, or athrow completion. It convertsargument to aninteger representing its Number value with fractional part truncated, or to +∞ or -∞ when that Number value is infinite. It performs the following steps when called:
𝔽(ToIntegerOrInfinity(x)) never returns-0𝔽 for any value ofx. The truncation of the fractional part is performed after convertingx to amathematical value.
Iff is even, return𝔽(f). Otherwise, return𝔽(f + 1).
Note
Unlike most other ECMAScriptinteger conversion operations, ToUint8Clamp rounds rather than truncates non-integral values. It also uses “round half to even” tie-breaking, which differs from the “round half up” tie-breaking ofMath.round.
7.1.13 ToBigInt (argument )
The abstract operation ToBigInt takes argumentargument (anECMAScript language value) and returns either anormal completion containing a BigInt or athrow completion. It convertsargument to a BigInt value, or throws if an implicit conversion from Number would be required. It performs the following steps when called:
The abstract operation CanonicalNumericIndexString takes argumentargument (a String) and returns a Number orundefined. Ifargument is either"-0" or exactly matchesToString(n) for some Number valuen, it returns the respective Number value. Otherwise, it returnsundefined. It performs the following steps when called:
The abstract operation IsCallable takes argumentargument (anECMAScript language value) and returns a Boolean. It determines ifargument is a callable function with a[[Call]] internal method. It performs the following steps when called:
Ifargument has a[[Call]] internal method, returntrue.
Returnfalse.
7.2.4 IsConstructor (argument )
The abstract operation IsConstructor takes argumentargument (anECMAScript language value) and returns a Boolean. It determines ifargument is afunction object with a[[Construct]] internal method. It performs the following steps when called:
Ifargument has a[[Construct]] internal method, returntrue.
Returnfalse.
7.2.5 IsExtensible (O )
The abstract operation IsExtensible takes argumentO (an Object) and returns either anormal completion containing a Boolean or athrow completion. It is used to determine whether additional properties can be added toO. It performs the following steps when called:
The abstract operation IsStringWellFormedUnicode takes argumentstring (a String) and returns a Boolean. It interpretsstring as a sequence of UTF-16 encoded code points, as described in6.1.4, and determines whether it is awell formed UTF-16 sequence. It performs the following steps when called:
The abstract operation SameType takes argumentsx (anECMAScript language value) andy (anECMAScript language value) and returns a Boolean. It determines whether or not the two arguments are the same type. It performs the following steps when called:
The abstract operation SameValue takes argumentsx (anECMAScript language value) andy (anECMAScript language value) and returns a Boolean. It determines whether or not the two arguments are the same value. It performs the following steps when called:
This algorithm differs from theIsStrictlyEqual Algorithm by treating allNaN values as equivalent and by differentiating+0𝔽 from-0𝔽.
7.2.10 SameValueZero (x,y )
The abstract operation SameValueZero takes argumentsx (anECMAScript language value) andy (anECMAScript language value) and returns a Boolean. It determines whether or not the two arguments are the same value (ignoring the difference between+0𝔽 and-0𝔽). It performs the following steps when called:
SameValueZero differs fromSameValue only in that it treats+0𝔽 and-0𝔽 as equivalent.
7.2.11 SameValueNonNumber (x,y )
The abstract operation SameValueNonNumber takes argumentsx (anECMAScript language value, but not a Number) andy (anECMAScript language value, but not a Number) and returns a Boolean. It performs the following steps when called:
For expository purposes, some cases are handled separately within this algorithm even if it is unnecessary to do so.
Note 2
The specifics of what "x isy" means are detailed in5.2.7.
7.2.12 IsLessThan (x,y,LeftFirst )
The abstract operation IsLessThan takes argumentsx (anECMAScript language value),y (anECMAScript language value), andLeftFirst (a Boolean) and returns either anormal completion containing either a Boolean orundefined, or athrow completion. It provides the semantics for the comparisonx <y, returningtrue,false, orundefined (which indicates that at least one operand isNaN). TheLeftFirst flag is used to control the order in which operations with potentially visible side-effects are performed uponx andy. It is necessary because ECMAScript specifies left to right evaluation of expressions. IfLeftFirst istrue, thex parameter corresponds to an expression that occurs to the left of they parameter's corresponding expression. IfLeftFirst isfalse, the reverse is the case and operations must be performed upony beforex. It performs the following steps when called:
Step3 differs from step1.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 UTF-16 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 but not in the same normalization form could test as unequal. Also note that lexicographic ordering bycode unit differs from ordering bycode point for Strings containingsurrogate pairs.
The abstract operation IsStrictlyEqual takes argumentsx (anECMAScript language value) andy (anECMAScript language value) and returns a Boolean. It provides the semantics for the=== operator. It performs the following steps when called:
This algorithm differs from theSameValue Algorithm in its treatment of signed zeroes and NaNs.
7.3 Operations on Objects
7.3.1 MakeBasicObject (internalSlotsList )
The abstract operation MakeBasicObject takes argumentinternalSlotsList (aList of internal slot names) and returns an Object. It is the source of all ECMAScript objects that are created algorithmically, including bothordinary objects andexotic objects. It factors out common steps used in creating all objects, and centralizes object creation. It performs the following steps when called:
SetinternalSlotsList to thelist-concatenation ofinternalSlotsList and «[[PrivateElements]] ».
Letobj be a newly created object with an internal slot for each name ininternalSlotsList.
Setobj's essential internal methods to the defaultordinary object definitions specified in10.1.
Assert: If the caller will not be overriding bothobj's[[GetPrototypeOf]] and[[SetPrototypeOf]] essential internal methods, theninternalSlotsList contains[[Prototype]].
Assert: If the caller will not be overriding all ofobj's[[SetPrototypeOf]],[[IsExtensible]], and[[PreventExtensions]] essential internal methods, theninternalSlotsList contains[[Extensible]].
Within this specification,exotic objects are created inabstract operations such asArrayCreate andBoundFunctionCreate 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 encapsulateexotic object creation, the object's essential internal methods are never modified outside those operations.
The abstract operation Set takes argumentsO (an Object),P (aproperty key),V (anECMAScript language value), andThrow (a Boolean) and returns either anormal completion containingunused or athrow completion. 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:
LetnewDesc 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 ifO is not extensible,[[DefineOwnProperty]] will returnfalse.
7.3.6 CreateDataPropertyOrThrow (O,P,V )
The abstract operation CreateDataPropertyOrThrow takes argumentsO (an Object),P (aproperty key), andV (anECMAScript language value) and returns either anormal completion containingunused or athrow completion. It is used to create a new own property of an object. It throws aTypeError 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 ifO is not extensible,[[DefineOwnProperty]] will returnfalse causing this operation to throw aTypeError exception.
The abstract operation CreateNonEnumerableDataPropertyOrThrow takes argumentsO (an Object),P (aproperty key), andV (anECMAScript language value) and returnsunused. It is used to create a new non-enumerable own property of anordinary object. It performs the following steps when called:
Assert:O is an ordinary, extensible object with no non-configurable properties.
LetnewDesc be the PropertyDescriptor {[[Value]]:V,[[Writable]]:true,[[Enumerable]]:false,[[Configurable]]:true }.
This abstract operation creates a property whose attributes are set to the same defaults used for properties created by the ECMAScript language assignment operator except it is not enumerable. Normally, the property will not already exist. If it does exist,DefinePropertyOrThrow is guaranteed to complete normally.
7.3.8 DefinePropertyOrThrow (O,P,desc )
The abstract operation DefinePropertyOrThrow takes argumentsO (an Object),P (aproperty key), anddesc (aProperty Descriptor) and returns either anormal completion containingunused or athrow completion. It is used to call the[[DefineOwnProperty]] internal method of an object in a manner that will throw aTypeError exception if the requested property update cannot be performed. It performs the following steps when called:
Letsuccess be ? O.[[DefineOwnProperty]](P,desc).
Ifsuccess isfalse, throw aTypeError exception.
Returnunused.
7.3.9 DeletePropertyOrThrow (O,P )
The abstract operation DeletePropertyOrThrow takes argumentsO (an Object) andP (aproperty key) and returns either anormal completion containingunused or athrow completion. 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 HasProperty takes argumentsO (an Object) andP (aproperty key) and returns either anormal completion containing a Boolean or athrow completion. It is used to determine whether an object has a property with the specifiedproperty key. The property may be either own or inherited. It performs the following steps when called:
Return ? O.[[HasProperty]](P).
7.3.12 HasOwnProperty (O,P )
The abstract operation HasOwnProperty takes argumentsO (an Object) andP (aproperty key) and returns either anormal completion containing a Boolean or athrow completion. It is used to determine whether an object has an own property with the specifiedproperty key. It performs the following steps when called:
The abstract operation Construct takes argumentF (aconstructor) and optional argumentsargumentsList (aList ofECMAScript language values) andnewTarget (aconstructor) and returns either anormal completion containing an Object or athrow completion. It is used to call the[[Construct]] internal method of afunction object.argumentsList andnewTarget are the values to be passed as the corresponding arguments of the internal method. IfargumentsList is not present, a new emptyList is used as its value. IfnewTarget is not present,F is used as its value. It performs the following steps when called:
IfnewTarget is not present, setnewTarget toF.
IfargumentsList is not present, setargumentsList to a new emptyList.
IfnewTarget is not present, this operation is equivalent to:new F(...argumentsList)
7.3.15 SetIntegrityLevel (O,level )
The abstract operation SetIntegrityLevel takes argumentsO (an Object) andlevel (sealed orfrozen) and returns either anormal completion containing a Boolean or athrow completion. 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 argumentsO (an Object) andlevel (sealed orfrozen) and returns either anormal completion containing a Boolean or athrow completion. It is used to determine if the set of own properties of an object are fixed. It performs the following steps when called:
The abstract operation CreateArrayFromList takes argumentelements (aList ofECMAScript language values) and returns an Array. It is used to create an Array whose elements are provided byelements. It performs the following steps when called:
The abstract operation LengthOfArrayLike takes argumentobj (an Object) and returns either anormal completion containing a non-negativeinteger or athrow completion. It returns the value of the"length" property of an array-like object. It performs the following steps when called:
The abstract operation CreateListFromArrayLike takes argumentobj (anECMAScript language value) and optional argumentvalidElementTypes (all orproperty-key) and returns either anormal completion containing aList ofECMAScript language values or athrow completion. It is used to create aList value whose elements are provided by the indexed properties ofobj.validElementTypes indicates the types of values that are allowed as elements. It performs the following steps when called:
IfvalidElementTypes is not present, setvalidElementTypes toall.
The target passed in here is always a newly created object which is not directly accessible in case of an error being thrown.
7.3.26 PrivateElementFind (O,P )
The abstract operation PrivateElementFind takes argumentsO (an Object) andP (aPrivate Name) and returns aPrivateElement orempty. It performs the following steps when called:
IfO.[[PrivateElements]] contains aPrivateElementpe such thatpe.[[Key]] isP, then
The abstract operation InitializeInstanceElements takes argumentsO (an Object) andconstructor (an ECMAScriptfunction object) and returns either anormal completion containingunused or athrow completion. It performs the following steps when called:
Letmethods be the value ofconstructor.[[PrivateMethods]].
The abstract operation IteratorComplete takes argumentiteratorResult (an Object) and returns either anormal completion containing a Boolean or athrow completion. It performs the following steps when called:
The abstract operation IteratorStep takes argumentiteratorRecord (anIterator Record) and returns either anormal completion containing either an Object ordone, or athrow completion. It requests the next value fromiteratorRecord.[[Iterator]] by callingiteratorRecord.[[NextMethod]] and returns eitherdone indicating that theiterator has reached its end or theIteratorResult object if a next value is available. It performs the following steps when called:
The abstract operation IteratorStepValue takes argumentiteratorRecord (anIterator Record) and returns either anormal completion containing either anECMAScript language value ordone, or athrow completion. It requests the next value fromiteratorRecord.[[Iterator]] by callingiteratorRecord.[[NextMethod]] and returns eitherdone indicating that theiterator has reached its end or the value from theIteratorResult object if a next value is available. It performs the following steps when called:
The abstract operation IteratorClose takes argumentsiteratorRecord (anIterator Record) andcompletion (aCompletion Record) and returns aCompletion Record. It is used to notify aniterator that it should perform any actions it would normally perform when it has reached its completed state. It performs the following steps when called:
The abstract operation AsyncIteratorClose takes argumentsiteratorRecord (anIterator Record) andcompletion (aCompletion Record) and returns aCompletion Record. It is used to notify anasync 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:
IfinnerResult.[[Value]]is not an Object, throw aTypeError exception.
Return ? completion.
7.4.14 CreateIteratorResultObject (value,done )
The abstract operation CreateIteratorResultObject takes argumentsvalue (anECMAScript language value) anddone (a Boolean) and returns an Object that conforms to theIteratorResult interface. It creates an object that conforms to theIteratorResult interface. It performs the following steps when called:
The abstract operation CreateListIteratorRecord takes argumentlist (aList ofECMAScript language values) and returns anIterator Record. It creates anIterator Record whose[[NextMethod]] returns the successive elements oflist. It performs the following steps when called:
Letclosure be a newAbstract Closure with no parameters that captureslist and performs the following steps when called: