In JScript, the assignment operator assigns a value to a variable. An equality operator compares two values.
Like many programming languages, JScript uses the equal sign (=) to assign values to variables: it is the assignment operator. The left operand of the = operator must be an Lvalue, which means that it must be a variable, array element, or object property.
The right operand of the = operator must be an Rvalue. An Rvalue can be an arbitrary value of any type, including a value that is the result of an expression. Following is an example of a JScript assignment statement.
anInteger = 3;
JScript interprets this statement as meaning:
"Assign the value 3 to the variable
anInteger takes the value 3."
An assignment will always be successful if type annotation has not bound the variable in the statement to a particular data type. Otherwise, the compiler will attempt to convert the Lvalue to the data type of the Rvalue. If the conversion can never be successfully performed, the compiler generates an error. If the conversion will succeed for some values but fail for others, the compiler generates a warning that the conversion may fail when the code is run.
In this example, the integer value stored in the variable
i is converted to a double value when assigned to the variable
var i : int = 29; var x : double = i;
For more information, see Type Conversion.
Unlike some other programming languages, JScript does not use the equal sign as a comparison operator, but only as the assignment operator. For comparison between two values, you can use either the equality operator (==) or the strict equality operator (===).
The equality operator compares primitive strings, numbers, and Booleans by value. If two variables have the same value, after type conversion (if necessary), the equality operator returns true. Objects (including Array, Function, String, Number, Boolean, Error,В Date and RegExp objects) compare by reference. Even if two object variables have the same value, the comparison returns true only if they refer to exactly the same object.
The strict equality operator compares both the value and type of two expressions; true is returned only if the two expressions compare as equal with the equality operator and the data type is the same for both operands.
The strict equality operator does not distinguish between the different numeric data types. Be certain you understand the difference between the assignment operator, the equality operator, and the strict equality operator.
Comparisons in your scripts always have a Boolean outcome. Consider the following line of JScript code.
y = (x == 2000);
Here, the value of the variable
x is tested to see if it is equal to the number 2000. If it is, the result of the comparison is the Boolean value true, which is assigned to the variable
x is not equal to 2000, then the result of the comparison is the Boolean value false, assigned to
The equality operator will type-convert to check if the values are the same. In the following line of JScript code, the literal string "42" will be converted to a number before comparing it to the number 42. The result is true.
42 == "42";
Objects are compared using different rules. The behavior of the equality operator depends on the type of the objects. If the objects are instances of a class that is defined with an equity operator, the returned value depends on the implementation of the equity operator. Classes that provide an equity operator cannot be defined in JScript, although other .NET Framework languages allow for such class definitions.
Objects without a defined equity operator, such as an object based on the JScript Object object or an instance of a JScript class, compare as equal only if both objects refer to the same object. This means that two distinct objects that contain the same data compare as different. The following example illustrates this behavior.
// A primitive string. var string1 = "Hello"; // Two distinct String objects with the same value. var StringObject1 = new String(string1); var StringObject2 = new String(string1); // An object converts to a primitive when // comparing an object and a primitive. print(string1 == StringObject1); // Prints true. // Two distinct objects compare as different. print(StringObject1 == StringObject2); // Prints false. // Use the toString() or valueOf() methods to compare object values. print(StringObject1.valueOf() == StringObject2); // Prints true.
Equality operators are especially useful in the condition statements of control structures. Here, you combine an equality operator with a statement that uses it. Consider the following JScript code sample.
if (x == 2000) z = z + 1; else x = x + 1;
The if...else statement in JScript performs one action (in this case,
z = z + 1) if the value of
x is 2000, and an alternate action (
x = x + 1) if the value of
x is not 2000. For more information, see JScript Conditional Structures.
The strict equality operator (===)performs type conversion only on numeric data types. This means that the integer 42 is considered to be identical to the double 42, but neither are identical to the string "42". This behavior is demonstrated by this JScript code.
var a : int = 42; var b : double = 42.00; var c : String = "42"; print(a===b); // Displays "true". print(a===c); // Displays "false". print(b===c); // Displays "false".