Comparison Operators
A comparison operator compares its operands and returns a logical value based on whether the comparison is true.
The operands can be numerical or string values. Strings are compared based on standard lexicographical ordering, using Unicode values.
A Boolean value is returned as the result of the comparison.
Two strings are equal when they have the same sequence of characters, same length, and same characters in corresponding positions.
Two numbers are equal when they are numerically equal (have the same number value). NaN is not equal to anything, including NaN. Positive and negative zeros are equal.
Two objects are equal if they refer to the same Object.
Two Boolean operands are equal if they are both true or false.
Null and Undefined types are equal.
The following table describes the comparison operators.
Operator | Description | Examples returning true 1 |
---|---|---|
Equal (==) | Returns true if the operands are equal. If the two operands are not of the same type, MetaScript attempts to convert the operands to an appropriate type for the comparison. | |
Not equal (!=) | Returns true if the operands are not equal. If the two operands are not of the same type, Metascript attempts to convert the operands to an appropriate type for the comparison. | |
Strict equal (===) | Returns true if the operands are equal and of the same type. | |
Strict not equal (!==) | Returns true if the operands are not equal and/or not of the same type. | |
Greater than (>) | Returns true if the left operand is greater than the right operand. | |
Greater than or equal (>=) | Returns true if the left operand is greater than or equal to the right operand. | |
Less than (<) | Returns true if the left operand is less than the right operand. | var1 < var2 |
Less than or equal (<=) | Returns true if the left operand is less than or equal to the right operand. | var1 <= var2 var2 <= 5 |
1 These examples assume that var1 has been assigned the value 3 and var2 has been assigned the value 4.
Using the Equality Operators
The standard equality operators (== and !=) compare two operands without regard to their type. The strict equality operators (=== and !==) perform equality comparisons on operands of the same type. Use strict equality operators if the operands must be of a specific type as well as value or if the exact type of the operands is important. Otherwise, use the standard equality operators, which allow you to compare the identity of two operands even if they are not of the same type.
When type conversion is needed, MetaScript converts String, Number, Boolean, or Object operands as follows.
- When comparing a number and a string, the string is converted to a number value. MetaScript attempts to convert the string numeric literal to a Number type value. First, a mathematical value is derived from the string numeric literal. Next, this value is rounded to nearest Number type value.
- If one of the operands is Boolean, the Boolean operand is converted to 1 if it is true and +0 if it is false.
- If an object is compared with a number or string, MetaScript attempts to return the default value for the object. Operators attempt to convert the object to a primitive value, a String or Number value, using the valueOf and toString methods of the objects. If this attempt to convert the object fails, a runtime error is generated.