Skip to content

11 Expressions

11.1 Primary Expressions

Syntax

PrimaryExpression :
this
Identifier
Literal
( Expression )

11.1.1 The this keyword

The this keyword evaluates to the this value of the execution context.

11.1.2 Identifier reference

An Identifier is evaluated using the scoping rules stated in section 10.1.4. The result of an Identifier is always a value of type Reference.

11.1.3 Literal reference

A Literal is evaluated as described in section 7.7.

11.1.4 The Grouping Operator

The production PrimaryExpression : ( Expression ) is evaluated as follows:

  1. Evaluate Expression. This may be of type Reference.
  2. Return Result(1).

NOTE This algorithm does not apply GetValue to Result(1). The principal motivation for this is so that operators such as delete and typeof may be applied to parenthesised expressions.

11.2 Left-Hand-Side Expressions

Syntax

MemberExpression :
PrimaryExpression
MemberExpression [ Expression ]
MemberExpression . Identifier
new MemberExpression Arguments
NewExpression :
MemberExpression
new NewExpression
CallExpression :
MemberExpression Arguments
CallExpression Arguments
CallExpression [ Expression ]
CallExpression . Identifier
Arguments :
()
( ArgumentList )
ArgumentList :
AssignmentExpression
ArgumentList , AssignmentExpression
LeftHandSideExpression :
NewExpression
CallExpression

11.2.1 Property Accessors

Properties are accessed by name, using either the dot notation:

MemberExpression . Identifier
CallExpression . Identifier

or the bracket notation:

MemberExpression [ Expression ]
CallExpression [ Expression ]

The dot notation is explained by the following syntactic conversion:

MemberExpression . Identifier

is identical in its behaviour to

MemberExpression [ <identifier-string> ]

and similarly

CallExpression . Identifier

is identical in its behaviour to

CallExpression [ <identifier-string> ]

where <identifier-string> is a string literal containing the same sequence of characters as the Identifier .

The production MemberExpression : MemberExpression [ Expression ] is evaluated as follows:

  1. Evaluate MemberExpression.
  2. Call GetValue(Result(1)).
  3. Evaluate Expression.
  4. Call GetValue(Result(3)).
  5. Call ToObject(Result(2)).
  6. Call ToString(Result(4)).
  7. Return a value of type Reference whose base object is Result(5) and whose property name is Result(6).

The production CallExpression : CallExpression [ Expression ] is evaluated in exactly the same manner, except that the contained CallExpression is evaluated in step 1.

11.2.2 The new operator

The production NewExpression : new NewExpression is evaluated as follows:

  1. Evaluate NewExpression.
  2. Call GetValue(Result(1)).
  3. If Type(Result(2)) is not Object, generate a runtime error.
  4. If Result(2) does not implement the internal [[Construct]] method, generate a runtime error.
  5. Call the [[Construct]] method on Result(2), providing no arguments (that is, an empty list of arguments).
  6. If Type(Result(5)) is not Object, generate a runtime error.
  7. Return Result(5).

The production MemberExpression : new MemberExpression Arguments is evaluated as follows:

  1. Evaluate MemberExpression.
  2. Call GetValue(Result(1)).
  3. Evaluate Arguments, producing an internal list of argument values (section 11.2.4).
  4. If Type(Result(2)) is not Object, generate a runtime error.
  5. If Result(2) does not implement the internal [[Construct]] method, generate a runtime error.
  6. Call the [[Construct]] method on Result(2), providing the list Result(3) as the argument values.
  7. If Type(Result(6)) is not Object, generate a runtime error.
  8. Return Result(6).

11.2.3 Function Calls

The production CallExpression : MemberExpression Arguments is evaluated as follows:

  1. Evaluate MemberExpression.
  2. Evaluate Arguments, producing an internal list of argument values (section 11.2.4).
  3. Call GetValue(Result(1)).
  4. If Type(Result(3)) is not Object, generate a runtime error.
  5. If Result(3) does not implement the internal [[Call]] method, generate a runtime error.
  6. If Type(Result(1)) is Reference, Result(6) is GetBase(Result(1)). Otherwise, Result(6) is null .
  7. If Result(6) is an activation object, Result(7) is null . Otherwise, Result(7) is the same as Result(6).
  8. Call the [[Call]] method on Result(3), providing Result(7) as the this value and providing the list Result(2) as the argument values.
  9. Return Result(8).

The production CallExpression : CallExpression Arguments is evaluated in exactly the same manner, except that the contained CallExpression is evaluated in step 1.

NOTE Result(8) will never be of type Reference if Result(3) is a native ECMAScript object. Whether calling a host object can return a value of type Reference is implementation-dependent.

11.2.4 Argument Lists

The evaluation of an argument list produces an internal list of values (section 8).

The production Arguments : ( ) is evaluated as follows:

1. Return an empty internal list of values.

The production Arguments : ( ArgumentList ) is evaluated as follows:

  1. Evaluate ArgumentList.
  2. Return Result(1).

The production ArgumentList : AssignmentExpression is evaluated as follows:

  1. Evaluate AssignmentExpression.
  2. Call GetValue(Result(1)).
  3. Return an internal list whose sole item is Result(2).

The production ArgumentList : ArgumentList , AssignmentExpression is evaluated as follows:

  1. Evaluate ArgumentList.
  2. Evaluate AssignmentExpression.
  3. Call GetValue(Result(2)).
  4. Return an internal list whose length is one greater than the length of Result(1) and whose items are the items of Result(1), in order, followed at the end by Result(3), which is the last item of the new list.

11.3 Postfix expressions

Syntax

PostfixExpression :
LeftHandSideExpression
LeftHandSideExpression [no LineTerminator here] ++
LeftHandSideExpression [no LineTerminator here] --

11.3.1 Postfix increment operator

The production MemberExpression : MemberExpression ++ is evaluated as follows:

  1. Evaluate MemberExpression.
  2. Call GetValue(Result(1)).
  3. Call ToNumber(Result(2)).
  4. Add the value 1 to Result(3), using the same rules as for the + operator (section Applying the additive operators ( +, - ) to numbers).
  5. Call PutValue(Result(1), Result(4)).
  6. Return Result(3).

11.3.2 Postfix decrement operator

The production MemberExpression : MemberExpression -- is evaluated as follows:

  1. Evaluate MemberExpression.
  2. Call GetValue(Result(1)).
  3. Call ToNumber(Result(2)).
  4. Subtract the value 1 from Result(3), using the same rules as for the - operator (section Applying the additive operators ( +, - ) to numbers).
  5. Call PutValue(Result(1), Result(4)).
  6. Return Result(3).

11.4 Unary operators

Syntax

UnaryExpression :
PostfixExpression
delete UnaryExpression
void UnaryExpression
typeof UnaryExpression
++ UnaryExpression
-- UnaryExpression
+ UnaryExpression
- UnaryExpression
~ UnaryExpression
! UnaryExpression

11.4.1 The delete operator

The production UnaryExpression : delete UnaryExpression is evaluated as follows:

  1. Evaluate UnaryExpression.
  2. Call GetBase(Result(1)).
  3. Call GetPropertyName(Result(1)).
  4. If Type(Result(2)) is not Object, return true .
  5. If Result(2) does not implement the internal [[Delete]] method, go to step 8.
  6. Call the [[Delete]] method on Result(2), providing Result(3) as the property name to delete.
  7. Return Result(6).
  8. Call the [[HasProperty]] method on Result(2), providing Result(3) as the property name to check for.
  9. If Result(8) is true , return false .
  10. Return true .

11.4.2 The void operator

The production UnaryExpression : void UnaryExpression is evaluated as follows:

  1. Evaluate UnaryExpression.
  2. Call GetValue(Result(1)).
  3. Return undefined .

11.4.3 The typeof operator

The production UnaryExpression : typeof UnaryExpression is evaluated as follows:

  1. Evaluate UnaryExpression.
  2. If Type(Result(1)) is Reference and GetBase(Result(1)) is null , return "undefined" .
  3. Call GetValue(Result(1)).
  4. Return a string determined by Type(Result(3)) according to the following table:
Type Result
Undefined "undefined"
Null "object"
Boolean "boolean"
Number "number"
String "string"
Object (native and
doesn’t implement [[Call]])
"object"
Object (native and
implements [[Call]])
"function"
Object (host) Implementation-dependent

11.4.4 Prefix increment operator

The production UnaryExpression : ++ UnaryExpression is evaluated as follows:

  1. Evaluate UnaryExpression.
  2. Call GetValue(Result(1)).
  3. Call ToNumber(Result(2)).
  4. Add the value 1 to Result(3), using the same rules as for the + operator (section 11.6.3).
  5. Call PutValue(Result(1), Result(4)).
  6. Return Result(4).

11.4.5 Prefix decrement operator

The production UnaryExpression : -- UnaryExpression is evaluated as follows:

  1. Evaluate UnaryExpression.
  2. Call GetValue(Result(1)).
  3. Call ToNumber(Result(2)).
  4. Subtract the value 1 from Result(3), using the same rules as for the - operator (section 11.6.3).
  5. Call PutValue(Result(1), Result(4)).
  6. Return Result(4).

11.4.6 Unary + operator

The unary + operator converts its operand to Number type.

The production UnaryExpression : + UnaryExpression is evaluated as follows:

  1. Evaluate UnaryExpression.
  2. Call GetValue(Result(1)).
  3. Call ToNumber(Result(2)).
  4. Return Result(3).

11.4.7 Unary - operator

The unary - operator converts its operand to Number type and then negates it. Note that negating +0 produces 0 , and negating 0 produces +0 .

The production UnaryExpression : - UnaryExpression is evaluated as follows:

  1. Evaluate UnaryExpression.
  2. Call GetValue(Result(1)).
  3. Call ToNumber(Result(2)).
  4. If Result(3) is NaN , return NaN .
  5. Negate Result(3); that is, compute a number with the same magnitude but opposite sign.
  6. Return Result(5).

11.4.8 The bitwise NOT operator ( ~ )

The production UnaryExpression : ~ UnaryExpression is evaluated as follows:

  1. Evaluate UnaryExpression.
  2. Call GetValue(Result(1)).
  3. Call ToInt32(Result(2)).
  4. Apply bitwise complement to Result(3). The result is a signed 32-bit integer.
  5. Return Result(4).

11.4.9 Logical NOT operator ( ! )

The production UnaryExpression : ! UnaryExpression is evaluated as follows:

  1. Evaluate UnaryExpression.
  2. Call GetValue(Result(1)).
  3. Call ToBoolean(Result(2)).
  4. If Result(3) is true , return false .
  5. Return true .

11.5 Multiplicative operators

Syntax

MultiplicativeExpression :
UnaryExpression
MultiplicativeExpression * UnaryExpression
MultiplicativeExpression / UnaryExpression
MultiplicativeExpression % UnaryExpression

Semantics

The production MultiplicativeExpression : MultiplicativeExpression @ UnaryExpression , where @ stands for one of the operators in the above definitions, is evaluated as follows:

  1. Evaluate MultiplicativeExpression.
  2. Call GetValue(Result(1)).
  3. Evaluate UnaryExpression.
  4. Call GetValue(Result(3)).
  5. Call ToNumber(Result(2)).
  6. Call ToNumber(Result(4)).
  7. Apply the specified operation (*, /, or %) to Result(5) and Result(6). See the notes below (11.5.1, 11.5.2, 11.5.3).
  8. Return Result(7).

11.5.1 Applying the * operator

The * operator performs multiplication, producing the product of its operands. Multiplication is commutative. Multiplication is not always associative in ECMAScript, because of finite precision.

The result of a floating-point multiplication is governed by the rules of IEEE 754 double-precision arithmetic:

  • -If either operand is NaN , the result is NaN .
  • - The sign of the result is positive if both operands have the same sign, negative if the operands have different signs.
  • -Multiplication of an infinity by a zero results in NaN .
  • - Multiplication of an infinity by an infinity results in an infinity. The sign is determined by the rule already stated above.
  • - Multiplication of an infinity by a finite non-zero value results in a signed infinity. The sign is determined by the rule already stated above.

- In the remaining cases, where neither an infinity or NaN is involved, the product is computed and rounded to the nearest representable value using IEEE 754 round-to-nearest mode. If the magnitude is too large to represent, the result is then an infinity of appropriate sign. If the magnitude is too small to represent, the result is then a zero of appropriate sign. The ECMAScript language requires support of gradual underflow as defined by IEEE 754.

11.5.2 Applying the / operator

The / operator performs division, producing the quotient of its operands. The left operand is the dividend and the right operand is the divisor. ECMAScript does not perform integer division. The operands and result of all division operations are double-precision floating-point numbers. The result of division is determined by the specification of IEEE 754 arithmetic:

  • -If either operand is NaN , the result is NaN .
  • - The sign of the result is positive if both operands have the same sign, negative if the operands have different signs.
  • -Division of an infinity by an infinity results in NaN .
  • - Division of an infinity by a zero results in an infinity. The sign is determined by the rule already stated above.
  • - Division of an infinity by a non-zero finite value results in a signed infinity. The sign is determined by the rule already stated above.
  • - Division of a finite value by an infinity results in zero. The sign is determined by the rule already stated above.
  • - Division of a zero by a zero results in NaN ; division of zero by any other finite value results in zero, with the sign determined by the rule already stated above.
  • - Division of a non-zero finite value by a zero results in a signed infinity. The sign is determined by the rule already stated above.
  • - In the remaining cases, where neither an infinity, nor a zero, nor NaN is involved, the quotient is computed and rounded to the nearest representable value using IEEE 754 round-to-nearest mode. If the magnitude is too large to represent, the operation overflows; the result is then an infinity of appropriate sign. If the magnitude is too small to represent, the operation underflows and the result is a zero of the appropriate sign. The ECMAScript language requires support of gradual underflow as defined by IEEE 754.

11.5.3 Applying the % operator

The binary % operator is said to yield the remainder of its operands from an implied division; the left operand is the dividend and the right operand is the divisor. In C and C++, the remainder operator accepts only integral operands, but in ECMAScript, it also accepts floating-point operands.

The result of a floating-point remainder operation as computed by the % operator is not the same as the "remainder" operation defined by IEEE 754. The IEEE 754 "remainder" operation computes the remainder from a rounding division, not a truncating division, and so its behaviour is not analogous to that of the usual integer remainder operator. Instead the ECMAScript language defines % on floatingpoint operations to behave in a manner analogous to that of the Java integer remainder operator; this may be compared with the C library function fmod.

The result of a ECMAScript floating-point remainder operation is determined by the rules of IEEE arithmetic:

  • -If either operand is NaN , the result is NaN .
  • -The sign of the result equals the sign of the dividend.
  • -If the dividend is an infinity, or the divisor is a zero, or both, the result is NaN .
  • -If the dividend is finite and the divisor is an infinity, the result equals the dividend.
  • -If the dividend is a zero and the divisor is finite, the result is the same as the dividend.

- In the remaining cases, where neither an infinity, nor a zero, nor NaN is involved, the floating-point remainder r from a dividend n and a divisor d is defined by the mathematical relation r = n (d * q) where q is an integer that is negative only if n/d is negative and positive only if n/d is positive, and whose magnitude is as large as possible without exceeding the magnitude of the true mathematical quotient of n and d.

11.6 Additive operators

Syntax

AdditiveExpression :
MultiplicativeExpression
AdditiveExpression + MultiplicativeExpression
AdditiveExpression - MultiplicativeExpression

11.6.1 The addition operator ( + )

The addition operator either performs string concatenation or numeric addition.

The production AdditiveExpression : AdditiveExpression + MultiplicativeExpression is evaluated as follows:

  1. Evaluate AdditiveExpression.
  2. Call GetValue(Result(1)).
  3. Evaluate MultiplicativeExpression.
  4. Call GetValue(Result(3)).
  5. Call ToPrimitive(Result(2)).
  6. Call ToPrimitive(Result(4)).
  7. If Type(Result(5)) is String or Type(Result(6)) is String, go to step 12. (Note that this step differs from step 3 in the algorithm for comparison for the relational operators in using or instead of and.)
  8. Call ToNumber(Result(5)).
  9. Call ToNumber(Result(6)).
  10. Apply the addition operation to Result(8) and Result(9). See the note below (11.6.3).
  11. Return Result(10).
  12. Call ToString(Result(5)).
  13. Call ToString(Result(6)).
  14. Concatenate Result(12) followed by Result(13).
  15. Return Result(14).

NOTE No hint is provided in the calls to ToPrimitive in steps 5 and 6. All native ECMAScript objects except Date objects handle the absence of a hint as if the hint Number were given; Date objects handle the absence of a hint as if the hint String were given. Host objects may handle the absence of a hint in some other manner.

11.6.2 The subtraction operator ( - )

The production AdditiveExpression : AdditiveExpression - MultiplicativeExpression is evaluated as follows:

  1. Evaluate AdditiveExpression.
  2. Call GetValue(Result(1)).
  3. Evaluate MultiplicativeExpression.
  4. Call GetValue(Result(3)).
  5. Call ToNumber(Result(2)).
  6. Call ToNumber(Result(4)).
  7. Apply the subtraction operation to Result(5) and Result(6). See the note below (11.6.3).
  8. Return Result(7).

11.6.3 Applying the additive operators ( +, - ) to numbers

The + operator performs addition when applied to two operands of numeric type, producing the sum of the operands. The - operator performs subtraction, producing the difference of two numeric operands.

Addition is a commutative operation, but not always associative.

The result of an addition is determined using the rules of IEEE 754 double-precision arithmetic:

  • -If either operand is NaN , the result is NaN .
  • -The sum of two infinities of opposite sign is NaN .
  • -The sum of two infinities of the same sign is the infinity of that sign.
  • -The sum of an infinity and a finite value is equal to the infinite operand.
  • - The sum of two negative zeros is 0 . The sum of two positive zeros, or of two zeros of opposite sign, is +0 .
  • -The sum of a zero and a nonzero finite value is equal to the nonzero operand.
  • -The sum of two nonzero finite values of the same magnitude and opposite sign is +0 .
  • - In the remaining cases, where neither an infinity, nor a zero, nor NaN is involved, and the operands have the same sign or have different magnitudes, the sum is computed and rounded to the nearest representable value using IEEE 754 round-to-nearest mode. If the magnitude is too large to represent, the operation overflows and the result is then an infinity of appropriate sign. The ECMAScript language requires support of gradual underflow as defined by IEEE 754.

The - operator performs subtraction when applied to two operands of numeric type, producing the difference of its operands; the left operand is the minuend and the right operand is the subtrahend. Given numeric operands a and b , it is always the case that a - b produces the same result as a +(- b ) .

11.7 Bitwise shift operators

Syntax

ShiftExpression :
AdditiveExpression
ShiftExpression << AdditiveExpression
ShiftExpression >> AdditiveExpression
ShiftExpression >>> AdditiveExpression

Semantics

The result of evaluating ShiftExpression is always truncated to 32 bits. If the result of evaluating ShiftExpression produces a fractional component, the fractional component is discarded. The result of evaluating an AdditiveExpresion that is the right-hand operand of a shift operator is always truncated to five bits.

11.7.1 The left shift operator ( << )

Performs a bitwise left shift operation on the left operand by the amount specified by the right operand.

The production ShiftExpression : ShiftExpression << AdditiveExpression is evaluated as follows:

  1. Evaluate ShiftExpression.
  2. Call GetValue(Result(1)).
  3. Evaluate AdditiveExpression.
  4. Call GetValue(Result(3)).
  5. Call ToInt32(Result(2)).
  6. Call ToUint32(Result(4)).
  7. Mask out all but the least significant 5 bits of Result(6), that is, compute Result(6) & 0x1F.
  8. Left shift Result(5) by Result(7) bits. The result is a signed 32 bit integer.
  9. Return Result(8).

11.7.2 The signed right shift operator ( >> )

Performs a sign-filling bitwise right shift operation on the left operand by the amount specified by the right operand.

The production ShiftExpression : ShiftExpression >> AdditiveExpression is evaluated as follows:

  1. Evaluate ShiftExpression.
  2. Call GetValue(Result(1)).
  3. Evaluate AdditiveExpression.
  4. Call GetValue(Result(3)).
  5. Call ToInt32(Result(2)).
  6. Call ToUint32(Result(4)).
  7. Mask out all but the least significant 5 bits of Result(6), that is, compute Result(6) & 0x1F.
  8. Perform sign-extending right shift of Result(5) by Result(7) bits. The most significant bit is propagated. The result is a signed 32 bit integer.
  9. Return Result(8).

11.7.3 The unsigned right shift operator ( >>> )

Performs a zero-filling bitwise right shift operation on the left operand by the amount specified by the right operand.

The production ShiftExpression : ShiftExpression >>> AdditiveExpression is evaluated as follows:

  1. Evaluate ShiftExpression.
  2. Call GetValue(Result(1)).
  3. Evaluate AdditiveExpression.
  4. Call GetValue(Result(3)).
  5. Call ToUint32(Result(2)).
  6. Call ToUint32(Result(4)).
  7. Mask out all but the least significant 5 bits of Result(6), that is, compute Result(6) & 0x1F.
  8. Perform zero-filling right shift of Result(5) by Result(7) bits. Vacated bits are filled with zero. The result is an unsigned 32 bit integer.
  9. Return Result(8).

11.8 Relational operators

Syntax

RelationalExpression :
ShiftExpression
RelationalExpression < ShiftExpression
RelationalExpression > ShiftExpression
RelationalExpression <= ShiftExpression
RelationalExpression >= ShiftExpression
RelationalExpression instanceof ShiftExpression
RelationalExpression in ShiftExpression

Semantics

The result of evaluating RelationalExpression is always of type Boolean, reflecting whether the relationship named by the operator holds between its two operands.

11.8.1 The less-than operator ( < )

The production RelationalExpression : RelationalExpression < ShiftExpression is evaluated as follows:

  1. Evaluate RelationalExpression.
  2. Call GetValue(Result(1)).
  3. Evaluate ShiftExpression.
  4. Call GetValue(Result(3)).
  5. Perform the comparison Result(2) < Result(4). (See section 11.8.5).
  6. If Result(5) is undefined , return false . Otherwise, return Result(5).

11.8.2 The greater-than operator ( > )

The production RelationalExpression : RelationalExpression > ShiftExpression is evaluated as follows:

  1. Evaluate RelationalExpression.
  2. Call GetValue(Result(1)).
  3. Evaluate ShiftExpression.
  4. Call GetValue(Result(3)).
  5. Perform the comparison Result(4) < Result(2). (See section 11.8.5).
  6. If Result(5) is undefined , return false . Otherwise, return Result(5).

11.8.3 The less-than-or-equal operator ( <= )

The production RelationalExpression : RelationalExpression <= ShiftExpression is evaluated as follows:

  1. Evaluate RelationalExpression.
  2. Call GetValue(Result(1)).
  3. Evaluate ShiftExpression.
  4. Call GetValue(Result(3)).
  5. Perform the comparison Result(4) < Result(2). (See section 11.8.5).
  6. If Result(5) is true or undefined , return false . Otherwise, return true .

11.8.4 The greater-than-or-equal operator ( >= )

The production RelationalExpression : RelationalExpression >= ShiftExpression is evaluated as follows:

  1. Evaluate RelationalExpression.
  2. Call GetValue(Result(1)).
  3. Evaluate ShiftExpression.
  4. Call GetValue(Result(3)).
  5. Perform the comparison Result(2) < Result(4). (See section 11.8.5).
  6. If Result(5) is true or undefined , return false . Otherwise, return true .

11.8.5 The abstract relational comparison algorithm

The comparison x < y , where x and y are values, produces true , false , or undefined (which indicates that at least one operand is NaN ). Such a comparison is performed as follows:

  1. Call ToPrimitive( x , hint Number).
  2. Call ToPrimitive( y , hint Number).
  3. If Type(Result(1)) is String and Type(Result(2)) is String, go to step 16. (Note that this step differs from step 7 in the algorithm for the addition operator + in using and instead of or .)
  4. Call ToNumber(Result(1)).
  5. Call ToNumber(Result(2)).
  6. If Result(4) is NaN , return undefined .
  7. If Result(5) is NaN , return undefined .
  8. If Result(4) and Result(5) are the same number value, return false .
  9. If Result(4) is +0 and Result(5) is 0 , return false .
  10. If Result(4) is 0 and Result(5) is +0 , return false .
  11. If Result(4) is + , return false .
  12. If Result(5) is + , return true .
  13. If Result(5) is , return false .
  14. If Result(4) is , return true .
  15. If the mathematical value of Result(4) is less than the mathematical value of Result(5)—note that these mathematical values are both finite and not both zero—return true . Otherwise, return false .
  16. If Result(2) is a prefix of Result(1), return false . (A string value p is a prefix of string value q if q can be the result of concatenating p and some other string r . Note that any string is a prefix of itself, because r may be the empty string.)
  17. If Result(1) is a prefix of Result(2), return true .
  18. Let k be the smallest nonnegative integer such that the character at position k within Result(1) is different from the character at position k within Result(2). (There must be such a k , for neither string is a prefix of the other.)
  19. Let m be the integer that is the Unicode encoding for the character at position k within Result(1).
  20. Let n be the integer that is the Unicode encoding for the character at position k within Result(2).
  21. If m < n , return true . Otherwise, return false .

NOTE The comparison of strings uses a simple lexicographic ordering on sequences of Unicode code point 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 2.0 specification.

11.9 Equality operators

Syntax

EqualityExpression :
RelationalExpression
EqualityExpression == RelationalExpression
EqualityExpression != RelationalExpression
EqualityExpression === RelationalExpression
EqualityExpression !== RelationalExpression

Semantics

The result of evaluating EqualityExpression is always of type Boolean, reflecting whether the relationship named by the operator holds between its two operands.

11.9.1 The equals operator ( == )

The production EqualityExpression : EqualityExpression == RelationalExpression is evaluated as follows:

  1. Evaluate EqualityExpression.
  2. Call GetValue(Result(1)).
  3. Evaluate RelationalExpression.
  4. Call GetValue(Result(3)).
  5. Perform the comparison Result(4) == Result(2). (See section 11.9.3).
  6. Return Result(5).

11.9.2 The does-not-equals operator ( != )

The production EqualityExpression : EqualityExpression != RelationalExpression is evaluated as follows:

  1. Evaluate EqualityExpression.
  2. Call GetValue(Result(1)).
  3. Evaluate RelationalExpression.
  4. Call GetValue(Result(3)).
  5. Perform the comparison Result(4) == Result(2). (See section 11.9.3).
  6. If Result(5) is true , return false . Otherwise, return true .

11.9.3 The abstract equality comparison algorithm

The comparison x == y , where x and y are values, produces true or false . Such a comparison is performed as follows:

  1. If Type( x ) is different from Type( y ), go to step 14.
  2. If Type( x ) is Undefined, return true .
  3. If Type( x ) is Null, return true .
  4. If Type( x ) is not Number, go to step 11.
  5. If x is NaN , return false .
  6. If y is NaN , return false .
  7. If x is the same number value as y , return true .
  8. If x is +0 and y is 0 , return true .
  9. If x is 0 and y is +0 , return true .
  10. Return false .
  11. If Type( x ) is String, then return true if x and y are exactly the same sequence of characters (same length and same characters in corresponding positions). Otherwise, return false ..
  12. If Type( x ) is Boolean, return true if x and y are both true or both false . Otherwise, return false .
  13. Return true if x and y refer to the same object. Otherwise, return false .
  14. If x is null and y is undefined , return true .
  15. If x is undefined and y is null , return true .
16. If Type(x) is Number and Type(y) is String,
   return the result of the comparison x == ToNumber(y).
17. If Type(x) is String and Type(y) is Number,
   return the result of the comparison ToNumber(x) == y.
18. If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y.
19. If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).
20. If Type(x) is either String or Number and Type(y) is Object,
   return the result of the comparison x == ToPrimitive(y).
21. If Type(x) is Object and Type(y) is either String or Number,
   return the result of the comparison ToPrimitive(x) == y.
22. Return false.

NOTE Given the above definition of equality::

String comparison can be forced by: "" + a == "" + b.

Numeric comparison can be forced by: a - 0 == b - 0 .

Boolean comparison can be forced by: !a == !b .

The equality operators maintain the following invariants:

  1. A != B is equivalent to !(A == B) .
  2. A == B is equivalent to B == A , except in the order of evaluation of A and B .

The equality operator is not always transitive. For example, there might be two distinct string objects, each representing the same string value; each string object would be considered equal to the string value by the == operator, but the two string objects would not be equal to each other.

Comparison of strings uses a simple equality test on sequences of Unicode code point 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 2.0 specification.

11.10 Binary bitwise operators

Syntax

BitwiseANDExpression :
EqualityExpression
BitwiseANDExpression & EqualityExpression
BitwiseXORExpression :
BitwiseANDExpression
BitwiseXORExpression ^ BitwiseANDExpression
BitwiseORExpression :
BitwiseXORExpression
BitwiseORExpression | BitwiseXORExpression

Semantics

The production A : A @ B , where @ is one of the bitwise operators in the productions above, is evaluated as follows:

  1. Evaluate A .
  2. Call GetValue(Result(1)).
  3. Evaluate B .
  4. Call GetValue(Result(3)).
  5. Call ToInt32(Result(2)).
  6. Call ToInt32(Result(4)).
  7. Apply the bitwise operator @ to Result(5) and Result(6). The result is a signed 32 bit integer.

8. Return Result(7).

11.11 Binary logical operators

Syntax

LogicalANDExpression :
BitwiseORExpression
LogicalANDExpression && BitwiseORExpression
LogicalORExpression :
LogicalANDExpression
LogicalORExpression || LogicalANDExpression

Semantics

The production LogicalANDExpression : LogicalANDExpression && BitwiseORExpression is evaluated as follows:

  1. Evaluate LogicalANDExpression.
  2. Call GetValue(Result(1)).
  3. Call ToBoolean(Result(2)).
  4. If Result(3) is false, return Result(2).
  5. Evaluate BitwiseORExpression.
  6. Call GetValue(Result(5)).
  7. Return Result(6).

The production LogicalORExpression : LogicalORExpression || LogicalANDExpression is evaluated as follows:

  1. Evaluate LogicalORExpression.
  2. Call GetValue(Result(1)).
  3. Call ToBoolean(Result(2)).
  4. If Result(3) is true, return Result(2).
  5. Evaluate LogicalANDExpression.
  6. Call GetValue(Result(5)).
  7. Return Result(6).

NOTE The value produced by a && or || operator is not necessarily of type Boolean. The value produced will always be the value of one of the two operand expressions.

11.12 Conditional operator ( ?: )

Syntax

ConditionalExpression :
LogicalORExpression
LogicalORExpression ? AssignmentExpression : AssignmentExpression

Semantics

The production ConditionalExpression : LogicalORExpression ? AssignmentExpression : AssignmentExpression is evaluated as follows:

  1. Evaluate LogicalORExpression.
  2. Call GetValue(Result(1)).
  3. Call ToBoolean(Result(2)).
  4. If Result(3) is false, go to step 8.
  5. Evaluate the first AssignmentExpression.
  6. Call GetValue(Result(5)).
  7. Return Result(6).
  8. Evaluate the second AssignmentExpression.
  9. Call GetValue(Result(8)).
  10. Return Result(9).

NOTE The grammar for a ConditionalExpression in ECMAScript is a little bit different from that in C and Java, which each allow the second subexpression to be an Expression but restrict the third expression to be a ConditionalExpression. The motivation for this difference in ECMAScript is to allow an assignment expression to be governed by either arm of a conditional and to eliminate the confusing and fairly useless case of a comma expression as the centre expression.

11.13 Assignment operators

Syntax

AssignmentExpression :
ConditionalExpression
LeftHandSideExpression AssignmentOperator AssignmentExpression
AssignmentOperator :one of
= *= /= %= += -= <<= >>= >>>= &= ^= |=

11.13.1 Simple Assignment ( = )

The production AssignmentExpression : LeftHandSideExpression = AssignmentExpression is evaluated as follows:

  1. Evaluate LeftHandSideExpression.
  2. Evaluate AssignmentExpression.
  3. Call GetValue(Result(2)).
  4. Call PutValue(Result(1), Result(3)).
  5. Return Result(3).

11.13.2 Compound assignment ( op= )

The production AssignmentExpression : LeftHandSideExpression @ = AssignmentExpression , where @ represents one of the operators indicated above, is evaluated as follows:

  1. Evaluate LeftHandSideExpression.
  2. Call GetValue(Result(1)).
  3. Evaluate AssignmentExpression.
  4. Call GetValue(Result(3)).
  5. Apply operator @ to Result(2) and Result(4).
  6. Call PutValue(Result(1), Result(5)).
  7. Return Result(5).

11.14 Comma operator ( , )

Syntax

Expression :
AssignmentExpression
Expression , AssignmentExpression

Semantics

The production Expression : Expression , AssignmentExpression is evaluated as follows:

  1. Evaluate Expression.
  2. Call GetValue(Result(1)).
  3. Evaluate AssignmentExpression.
  4. Call GetValue(Result(3)).
  5. Return Result(4).