19 The Global Object
The global object:
- is created before control enters any
execution context . - does not have a [[Construct]] internal method; it cannot be used as a
constructor with thenewoperator. - does not have a [[Call]] internal method; it cannot be invoked as a function.
- has a [[Prototype]] internal slot whose value is
host-defined . - may have
host-defined properties in addition to the properties defined in this specification. This may include a property whose value is the global object itself.
19.1 Value Properties of the Global Object
19.1.1 globalThis
The initial value of the
This property has the attributes { [[Writable]]:
19.1.2 Infinity
The initial value of the
This property has the attributes { [[Writable]]:
19.1.3 NaN
The initial value of the
This property has the attributes { [[Writable]]:
19.1.4 undefined
The initial value of the
This property has the attributes { [[Writable]]:
19.2 Function Properties of the Global Object
19.2.1 eval ( source )
This function is the
It performs the following steps when called:
- Return ?
PerformEval (source,false ,false ).
19.2.1.1 PerformEval ( source, strictCaller, direct )
The abstract operation PerformEval takes arguments source (an
Assert : If direct isfalse , then strictCaller is alsofalse .- If source
is not a String , return source. - Let evalRealm be
the current Realm Record . NOTE : In the case of adirect eval , evalRealm is therealm of both the caller ofevaland of theevalfunction itself.- Perform ?
HostEnsureCanCompileStrings (evalRealm, « », source, direct). - Let inFunc be
false . - Let inMethod be
false . - Let inDerivedCtor be
false . - Let inClassFieldInitializer be
false . - If direct is
true , then- Let thisEnvRecord be
GetThisEnvironment (). - If thisEnvRecord is a
Function Environment Record , then- Let func be thisEnvRecord.[[FunctionObject]].
- Set inFunc to
true . - Set inMethod to thisEnvRecord.HasSuperBinding().
- If func.[[ConstructorKind]] is
derived , set inDerivedCtor totrue . - Let classFieldInitializerName be func.[[ClassFieldInitializerName]].
- If classFieldInitializerName is not
empty , set inClassFieldInitializer totrue .
- Let thisEnvRecord be
- Perform the following substeps in an
implementation-defined order, possibly interleaving parsing and error detection:- Let script be
ParseText (source,Script ). - If script is a
List of errors, throw aSyntaxError exception. - If script
Contains ScriptBody isfalse , returnundefined . - Let body be the
ScriptBody of script. - If inFunc is
false and bodyContains NewTarget , throw aSyntaxError exception. - If inMethod is
false and bodyContains SuperProperty , throw aSyntaxError exception. - If inDerivedCtor is
false and bodyContains SuperCall , throw aSyntaxError exception. - If inClassFieldInitializer is
true andContainsArguments of body istrue , throw aSyntaxError exception.
- Let script be
- If strictCaller is
true , let strictEval betrue . - Else, let strictEval be
ScriptIsStrict of script. - Let runningContext be the
running execution context . NOTE : If direct istrue , runningContext will be theexecution context that performed thedirect eval . If direct isfalse , runningContext will be theexecution context for the invocation of theevalfunction.- If direct is
true , then- Let lexicalEnv be
NewDeclarativeEnvironment (runningContext's LexicalEnvironment). - Let variableEnv be runningContext's VariableEnvironment.
- Let privateEnv be runningContext's PrivateEnvironment.
- Let lexicalEnv be
- Else,
- Let lexicalEnv be
NewDeclarativeEnvironment (evalRealm.[[GlobalEnv]]). - Let variableEnv be evalRealm.[[GlobalEnv]].
- Let privateEnv be
null .
- Let lexicalEnv be
- If strictEval is
true , set variableEnv to lexicalEnv. - If runningContext is not already suspended, suspend runningContext.
- Let evalContext be a new
ECMAScript code execution context . - Set evalContext's Function to
null . - Set evalContext's
Realm to evalRealm. - Set evalContext's ScriptOrModule to runningContext's ScriptOrModule.
- Set evalContext's VariableEnvironment to variableEnv.
- Set evalContext's LexicalEnvironment to lexicalEnv.
- Set evalContext's PrivateEnvironment to privateEnv.
- Push evalContext onto the
execution context stack ; evalContext is now therunning execution context . - Let result be
Completion (EvalDeclarationInstantiation (body, variableEnv, lexicalEnv, privateEnv, strictEval)). - If result is a
normal completion , then- Set result to
Completion (Evaluation of body).
- Set result to
- If result is a
normal completion and result.[[Value]] isempty , then- Set result to
NormalCompletion (undefined ).
- Set result to
- Suspend evalContext and remove it from the
execution context stack . - Resume the context that is now on the top of the
execution context stack as therunning execution context . - Return ? result.
The eval code cannot instantiate variable or function bindings in the variable environment of the calling context that invoked the eval if either the code of the calling context or the eval code is let, const, or class declarations are always instantiated in a new LexicalEnvironment.
19.2.1.2 HostEnsureCanCompileStrings ( calleeRealm, paramStrings, bodyString, direct )
The
paramStrings represents the strings that, when using one of the function eval call.
direct signifies whether the evaluation is a
The default implementation of HostEnsureCanCompileStrings is to return
19.2.1.3 EvalDeclarationInstantiation ( body, variableEnv, lexicalEnv, privateEnv, strict )
The abstract operation EvalDeclarationInstantiation takes arguments body (a
- Let variableNames be the
VarDeclaredNames of body. - Let variableDecls be the
VarScopedDeclarations of body. - If strict is
false , then- If variableEnv is a
Global Environment Record , then- For each element name of variableNames, do
- If
HasLexicalDeclaration (variableEnv, name) istrue , throw aSyntaxError exception. NOTE :evalwill not create a global var declaration that would be shadowed by a global lexical declaration.
- If
- For each element name of variableNames, do
- Let thisEnv be lexicalEnv.
Assert : The following loop will terminate.- Repeat, while thisEnv and variableEnv are not the same
Environment Record ,- If thisEnv
is not an Object Environment Record , thenNOTE : The environment of with statements cannot contain any lexical declaration so it doesn't need to be checked for var/let hoisting conflicts.- For each element name of variableNames, do
- If ! thisEnv.HasBinding(name) is
true , then- If the
host is a web browser orotherwise supports VariableStatements in Catch Blocks , then- If thisEnv is not the
Environment Record for aCatch clause, throw aSyntaxError exception.
- If thisEnv is not the
- Else,
- Throw a
SyntaxError exception.
- Throw a
- If the
NOTE : Adirect eval will not hoist var declaration over a like-named lexical declaration.
- If ! thisEnv.HasBinding(name) is
- Set thisEnv to thisEnv.[[OuterEnv]].
- If thisEnv
- If variableEnv is a
- Let privateIdentifiers be a new empty
List . - Let pointer be privateEnv.
- Repeat, while pointer is not
null ,- For each
Private Name binding of pointer.[[Names]], do- If privateIdentifiers does not contain binding.[[Description]], append binding.[[Description]] to privateIdentifiers.
- Set pointer to pointer.[[OuterPrivateEnvironment]].
- For each
- If
AllPrivateIdentifiersValid of body with argument privateIdentifiers isfalse , throw aSyntaxError exception. - Let funcsToInitialize be a new empty
List . - Let declaredFuncNames be a new empty
List . - For each element d of variableDecls, in reverse
List order, do- If d is not either a
VariableDeclaration , aForBinding , or aBindingIdentifier , thenAssert : d is either aFunctionDeclaration , aGeneratorDeclaration , anAsyncFunctionDeclaration , or anAsyncGeneratorDeclaration .NOTE : If there are multiple function declarations for the same name, the last declaration is used.- Let func be the sole element of the
BoundNames of d. - If declaredFuncNames does not contain func, then
- If variableEnv is a
Global Environment Record , then- Let funcDefinable be ?
CanDeclareGlobalFunction (variableEnv, func). - If funcDefinable is
false , throw aTypeError exception.
- Let funcDefinable be ?
- Append func to declaredFuncNames.
- Insert d as the first element of funcsToInitialize.
- If variableEnv is a
- If d is not either a
- Let declaredVariableNames be a new empty
List . - For each element d of variableDecls, do
- If d is either a
VariableDeclaration , aForBinding , or aBindingIdentifier , then- For each String vn of the
BoundNames of d, do- If declaredFuncNames does not contain vn, then
- If variableEnv is a
Global Environment Record , then- Let vnDefinable be ?
CanDeclareGlobalVar (variableEnv, vn). - If vnDefinable is
false , throw aTypeError exception.
- Let vnDefinable be ?
- If declaredVariableNames does not contain vn, then
- Append vn to declaredVariableNames.
- If variableEnv is a
- If declaredFuncNames does not contain vn, then
- For each String vn of the
- If d is either a
- If strict is
false and thehost is a web browser orotherwise supports Block-Level Function Declarations Web Legacy Compatibility Semantics , then- Let declaredFuncOrVariableNames be the
list-concatenation of declaredFuncNames and declaredVariableNames. - For each
FunctionDeclaration f that is directly contained in theStatementList of anyBlock ,CaseClause , orDefaultClause x such that bodyContains x istrue , do- Let funcName be the
StringValue of theBindingIdentifier of f. - If replacing the
FunctionDeclaration f with aVariableStatement that has funcName as aBindingIdentifier would not produce any Early Errors for body, then- Let bindingExists be
false . - Set thisEnv to lexicalEnv.
Assert : The following loop will terminate.- Repeat, while thisEnv is not variableEnv,
- If thisEnv
is not an Object Environment Record , then- If ! thisEnv.HasBinding(funcName) is
true , then- If the
host is a web browser orotherwise supports VariableStatements in Catch Blocks , then- If thisEnv is not the
Environment Record for aCatch clause, set bindingExists totrue .
- If thisEnv is not the
- Else,
- Set bindingExists to
true .
- Set bindingExists to
- If the
- If ! thisEnv.HasBinding(funcName) is
- Set thisEnv to thisEnv.[[OuterEnv]].
- If thisEnv
- If bindingExists is
false and variableEnv is aGlobal Environment Record , then- If
HasLexicalDeclaration (variableEnv, funcName) isfalse , then- Let funcDefinable be ?
CanDeclareGlobalVar (variableEnv, funcName).
- Let funcDefinable be ?
- Else,
- Let funcDefinable be
false .
- Let funcDefinable be
- If
- Else,
- Let funcDefinable be
true .
- Let funcDefinable be
- If bindingExists is
false and funcDefinable istrue , then- If declaredFuncOrVariableNames does not contain funcName, then
- If variableEnv is a
Global Environment Record , then- Perform ?
CreateGlobalVarBinding (variableEnv, funcName,true ).
- Perform ?
- Else,
- Set bindingExists to ! variableEnv.HasBinding(funcName).
- If bindingExists is
false , then- Perform ! variableEnv.CreateMutableBinding(funcName,
true ). - Perform ! variableEnv.InitializeBinding(funcName,
undefined ).
- Perform ! variableEnv.CreateMutableBinding(funcName,
- Append funcName to declaredFuncOrVariableNames.
- If variableEnv is a
- When the
FunctionDeclaration f is evaluated, perform the following steps in place of theFunctionDeclaration Evaluation algorithm provided in15.2.6 :- Let gEnv be the
running execution context 's VariableEnvironment. - Let bEnv be the
running execution context 's LexicalEnvironment. - Let fObj be ! bEnv.GetBindingValue(funcName,
false ). - Perform ?
gEnv.SetMutableBinding(funcName, fObj, .false ) - Return
unused .
- Let gEnv be the
- If declaredFuncOrVariableNames does not contain funcName, then
- Let bindingExists be
- Let funcName be the
- Let declaredFuncOrVariableNames be the
NOTE : No abnormal terminations occur after this algorithm step unless variableEnv is aGlobal Environment Record and theglobal object is aProxy exotic object .- Let lexicalDecls be the
LexicallyScopedDeclarations of body. - For each element d of lexicalDecls, do
NOTE : Lexically declared names are only instantiated here but not initialized.- For each element dn of the
BoundNames of d, do- If
IsConstantDeclaration of d istrue , then- Perform ? lexicalEnv.CreateImmutableBinding(dn,
true ).
- Perform ? lexicalEnv.CreateImmutableBinding(dn,
- Else,
- Perform ? lexicalEnv.CreateMutableBinding(dn,
false ).
- Perform ? lexicalEnv.CreateMutableBinding(dn,
- If
- For each
Parse Node f of funcsToInitialize, do- Let func be the sole element of the
BoundNames of f. - Let funcObj be
InstantiateFunctionObject of f with arguments lexicalEnv and privateEnv. - If variableEnv is a
Global Environment Record , then- Perform ?
CreateGlobalFunctionBinding (variableEnv, func, funcObj,true ).
- Perform ?
- Else,
- Let bindingExists be ! variableEnv.HasBinding(func).
- If bindingExists is
false , thenNOTE : The following invocation cannot return anabrupt completion because of the validation preceding step14 .- Perform ! variableEnv.CreateMutableBinding(func,
true ). - Perform ! variableEnv.InitializeBinding(func, funcObj).
- Else,
- Perform ! variableEnv.SetMutableBinding(func, funcObj,
false ).
- Perform ! variableEnv.SetMutableBinding(func, funcObj,
- Let func be the sole element of the
- For each String vn of declaredVariableNames, do
- If variableEnv is a
Global Environment Record , then- Perform ?
CreateGlobalVarBinding (variableEnv, vn,true ).
- Perform ?
- Else,
- Let bindingExists be ! variableEnv.HasBinding(vn).
- If bindingExists is
false , thenNOTE : The following invocation cannot return anabrupt completion because of the validation preceding step14 .- Perform ! variableEnv.CreateMutableBinding(vn,
true ). - Perform ! variableEnv.InitializeBinding(vn,
undefined ).
- If variableEnv is a
- Return
unused .
19.2.2 isFinite ( value )
19.2.3 isNaN ( value )
This function is the
It performs the following steps when called:
- Let number be ?
ToNumber (value). - If number is
NaN , returntrue . - Return
false .
A reliable way for ECMAScript code to test if a value X is X !== X. The result will be X is
19.2.4 parseFloat ( string )
This function produces a Number value dictated by interpretation of the contents of the string argument as a decimal literal.
It is the
It performs the following steps when called:
- Let inputString be ?
ToString (string). - Let trimmedString be !
TrimString (inputString,start ). - Let trimmed be
StringToCodePoints (trimmedString). - Let trimmedPrefix be the longest prefix of trimmed that satisfies the syntax of a
StrDecimalLiteral , which might be trimmed itself. If there is no such prefix, returnNaN . - Let parsedNumber be
ParseText (trimmedPrefix,StrDecimalLiteral ). Assert : parsedNumber is aParse Node .- Return the
StringNumericValue of parsedNumber.
This function may interpret only a leading portion of string as a Number value; it ignores any code units that cannot be interpreted as part of the notation of a decimal literal, and no indication is given that any such code units were ignored.
19.2.5 parseInt ( string, radix )
This function produces an
It is the
It performs the following steps when called:
- Let inputString be ?
ToString (string). - Let trimmedString be !
TrimString (inputString,start ). - Let sign be 1.
- If trimmedString is not empty and the first code unit of trimmedString is the code unit 0x002D (HYPHEN-MINUS), set sign to -1.
- If trimmedString is not empty and the first code unit of trimmedString is either the code unit 0x002B (PLUS SIGN) or the code unit 0x002D (HYPHEN-MINUS), set trimmedString to the
substring of trimmedString from index 1. - Let radixMV be
ℝ (?ToInt32 (radix)). - Let stripPrefix be
true . - If radixMV ≠ 0, then
- If radixMV < 2 or radixMV > 36, return
NaN . - If radixMV ≠ 16, set stripPrefix to
false .
- If radixMV < 2 or radixMV > 36, return
- Else,
- Set radixMV to 10.
- If stripPrefix is
true , then- If the length of trimmedString ≥ 2 and the first two code units of trimmedString are either
"0x" or"0X" , then- Set trimmedString to the
substring of trimmedString from index 2. - Set radixMV to 16.
- Set trimmedString to the
- If the length of trimmedString ≥ 2 and the first two code units of trimmedString are either
- If trimmedString contains a code unit that is not a radix-radixMV digit, let end be the index within trimmedString of the first such code unit; else let end be the length of trimmedString.
- Let numberString be the
substring of trimmedString from 0 to end. - If numberString is empty, return
NaN . - Let mathInt be the
integer value that is represented by numberString in radix-radixMV notation, using the letters A through Z and a through z for digits with values 10 through 35. (However, if radixMV = 10 and numberString contains more than 20 significant digits, every significant digit after the 20th may be replaced by a 0 digit, at the option of the implementation; and if radixMV is not one of 2, 4, 8, 10, 16, or 32, then mathInt may be animplementation-approximated integer representing theinteger value denoted by numberString in radix-radixMV notation.) - If mathInt = 0, then
- If sign = -1, return
-0 𝔽. - Return
+0 𝔽.
- If sign = -1, return
- Return
𝔽 (sign × mathInt).
19.2.6 URI Handling Functions
Uniform Resource Identifiers, or URIs, are Strings that identify resources (e.g. web pages or files) and transport protocols by which to access them (e.g. HTTP or FTP) on the Internet. The ECMAScript language itself does not provide any support for using URIs except for functions that encode and decode URIs as described in this section. encodeURI and decodeURI are intended to work with complete URIs; they assume that any reserved characters are intended to have special meaning (e.g., as delimiters) and so are not encoded. encodeURIComponent and decodeURIComponent are intended to work with the individual components of a URI; they assume that any reserved characters represent text and must be encoded to avoid special meaning when the component is part of a complete URI.
The set of reserved characters is based upon RFC 2396 and does not reflect changes introduced by the more recent RFC 3986.
Many implementations of ECMAScript provide additional functions and methods that manipulate web pages; these functions are beyond the scope of this standard.
19.2.6.1 decodeURI ( encodedURI )
This function computes a new version of a URI in which each escape sequence and UTF-8 encoding of the sort that might be introduced by the encodeURI function is replaced with the UTF-16 encoding of the code point that it represents. Escape sequences that could not have been introduced by encodeURI are not replaced.
It is the
It performs the following steps when called:
19.2.6.2 decodeURIComponent ( encodedURIComponent )
This function computes a new version of a URI in which each escape sequence and UTF-8 encoding of the sort that might be introduced by the encodeURIComponent function is replaced with the UTF-16 encoding of the code point that it represents.
It is the
It performs the following steps when called:
19.2.6.3 encodeURI ( uri )
This function computes a new version of a UTF-16 encoded (
It is the
It performs the following steps when called:
19.2.6.4 encodeURIComponent ( uriComponent )
This function computes a new version of a UTF-16 encoded (
It is the
It performs the following steps when called:
19.2.6.5 Encode ( string, extraUnescaped )
The abstract operation Encode takes arguments string (a String) and extraUnescaped (a String) and returns either a
- Let length be the length of string.
- Let result be the empty String.
- Let alwaysUnescaped be the
string-concatenation ofthe ASCII word characters and"-.!~*'()" . - Let unescapedSet be the
string-concatenation of alwaysUnescaped and extraUnescaped. - Let k be 0.
- Repeat, while k < length,
- Let codeUnit be the code unit at index k within string.
- If unescapedSet contains codeUnit, then
- Set k to k + 1.
- Set result to the
string-concatenation of result and codeUnit.
- Else,
- Let codePoint be
CodePointAt (string, k). - If codePoint.[[IsUnpairedSurrogate]] is
true , throw aURIError exception. - Set k to k + codePoint.[[CodeUnitCount]].
- Let octets be the
List of octets resulting by applying the UTF-8 transformation to codePoint.[[CodePoint]]. - For each element octet of octets, do
- Let hex be the String representation of octet, formatted as an uppercase hexadecimal number.
- Set result to the
string-concatenation of result,"%" , andStringPad (hex, 2,"0" ,start ).
- Let codePoint be
- Return result.
Because percent-encoding is used to represent individual octets, a single code point may be expressed as multiple consecutive escape sequences (one for each of its 8-bit UTF-8 code units).
19.2.6.6 Decode ( string, preserveEscapeSet )
The abstract operation Decode takes arguments string (a String) and preserveEscapeSet (a String) and returns either a
- Let length be the length of string.
- Let result be the empty String.
- Let k be 0.
- Repeat, while k < length,
- Let codeUnit be the code unit at index k within string.
- Let segment be codeUnit.
- If codeUnit is the code unit 0x0025 (PERCENT SIGN), then
- If k + 3 > length, throw a
URIError exception. - Let escape be the
substring of string from k to k + 3. - Let firstOctet be
ParseHexOctet (string, k + 1). - If firstOctet is not an
integer , throw aURIError exception. - Set k to k + 2.
- Let n be the number of leading 1 bits in firstOctet.
- If n = 0, then
- Let asciiChar be the code unit whose numeric value is firstOctet.
- If preserveEscapeSet contains asciiChar, set segment to escape; else set segment to asciiChar.
- Else,
- If n = 1 or n > 4, throw a
URIError exception. - Let octets be « firstOctet ».
- Let j be 1.
- Repeat, while j < n,
- Set k to k + 1.
- If k + 3 > length, throw a
URIError exception. - If the code unit at index k within string is not the code unit 0x0025 (PERCENT SIGN), throw a
URIError exception. - Let continuationByte be
ParseHexOctet (string, k + 1). - If continuationByte is not an
integer , throw aURIError exception. - Append continuationByte to octets.
- Set k to k + 2.
- Set j to j + 1.
Assert : The length of octets is n.- If octets does not contain a valid UTF-8 encoding of a Unicode code point, throw a
URIError exception. - Let codePoint be the code point obtained by applying the UTF-8 transformation to octets, that is, from a
List of octets into a 21-bit value. - Set segment to
UTF16EncodeCodePoint (codePoint).
- If n = 1 or n > 4, throw a
- If k + 3 > length, throw a
- Set result to the
string-concatenation of result and segment. - Set k to k + 1.
- Return result.
RFC 3629 prohibits the decoding of invalid UTF-8 octet sequences. For example, the invalid sequence 0xC0 0x80 must not decode into the code unit 0x0000. Implementations of the Decode algorithm are required to throw a
19.2.6.7 ParseHexOctet ( string, position )
The abstract operation ParseHexOctet takes arguments string (a String) and position (a non-negative
- Let length be the length of string.
Assert : position + 2 ≤ length.- Let hexDigits be the
substring of string from position to position + 2. - Let parseResult be
ParseText (hexDigits,HexDigits ).[~Sep] - If parseResult is not a
Parse Node , return parseResult. - Let n be the MV of parseResult.
Assert : n is in theinclusive interval from 0 to 255.- Return n.
19.3 Constructor Properties of the Global Object
19.3.1 AggregateError ( . . . )
See
19.3.2 Array ( . . . )
See
19.3.3 ArrayBuffer ( . . . )
See
19.3.4 BigInt ( . . . )
See
19.3.5 BigInt64Array ( . . . )
See
19.3.6 BigUint64Array ( . . . )
See
19.3.7 Boolean ( . . . )
See
19.3.8 DataView ( . . . )
See
19.3.9 Date ( . . . )
See
19.3.10 Error ( . . . )
See
19.3.11 EvalError ( . . . )
See
19.3.12 FinalizationRegistry ( . . . )
See
19.3.13 Float16Array ( . . . )
See
19.3.14 Float32Array ( . . . )
See
19.3.15 Float64Array ( . . . )
See
19.3.16 Function ( . . . )
See
19.3.17 Int8Array ( . . . )
See
19.3.18 Int16Array ( . . . )
See
19.3.19 Int32Array ( . . . )
See
19.3.20 Iterator ( . . . )
See
19.3.21 Map ( . . . )
See
19.3.22 Number ( . . . )
See
19.3.23 Object ( . . . )
See
19.3.24 Promise ( . . . )
See
19.3.25 Proxy ( . . . )
See
19.3.26 RangeError ( . . . )
See
19.3.27 ReferenceError ( . . . )
See
19.3.28 RegExp ( . . . )
See
19.3.29 Set ( . . . )
See
19.3.30 SharedArrayBuffer ( . . . )
See
19.3.31 String ( . . . )
See
19.3.32 Symbol ( . . . )
See
19.3.33 SyntaxError ( . . . )
See
19.3.34 TypeError ( . . . )
See
19.3.35 Uint8Array ( . . . )
See
19.3.36 Uint8ClampedArray ( . . . )
See
19.3.37 Uint16Array ( . . . )
See
19.3.38 Uint32Array ( . . . )
See
19.3.39 URIError ( . . . )
See
19.3.40 WeakMap ( . . . )
See
19.3.41 WeakRef ( . . . )
See
19.3.42 WeakSet ( . . . )
See
19.4 Other Properties of the Global Object
19.4.1 Atomics
See
19.4.2 JSON
See
19.4.3 Math
See
19.4.4 Reflect
See