The Equality and Relational Operators in ECMAScript 2015
ECMAScript Operators – Part 3
ECMAScript 6
Foreword: In this part of the series I explain the meaning of equality, identical and relational operators in ECMAScript.
By: Chrysanthus Date Published: 13 May 2016
Introduction
The Equals Operator
It is ==, typed as a double assignment operator. It is not = which is the assignment operator. The equals operator returns true if the operands on either sides are equal, otherwise it returns false. You should have seen examples of its use before.
The Does-Not-Equals Operator
The Does-Not-Equals operator is the opposite of the Equals Operator. The Does-Not-Equals operator is, != . It returns true if the operands are not equal, otherwise it returns false. Let us look at some examples:
Try the following code:
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<script type="text/ECMAScript">
myVar = 25;
hisVar = 30;
if (myVar != hisVar)
{
alert("The values of the two identifiers are not equal.");
}
</script>
</body>
</html>
The extra HTML code is to produce a web page.
myVar is 25, hisVar is 30. The condition is read like this: If myVar is not equal to hisVar, then the if-block will be executed. Since the values of the identifiers are not equal, (myVar != myVar) results in true.
In the following code, the values of the two identifiers are equal, so the condition returns false and the if-block is not executed.
<script type="text/ECMAScript">
myVar = 50;
hisVar = 50;
if (myVar != hisVar)
{
alert("The values of the two identifiers are not equal.");
}
</script>
<> is also an operator. It is synonymous to !=.
The Strict Equals Operator
The Strict Equals Operator is ===. It is similar to the Equals Operator, but here, it is not only the two values that have to be equal; the types of the two values should also be equal. 2.5 and "2.5" as operands return true for the Equals Operator, but they return false for the Strict Equals Operator. This is because 2.5 and "2.5" have the same value but they do not have the same type, since 2.5 is of data type, number and "2.5" is of data type, string. Read and try the following code:
<script type="text/ECMAScript">
myVar = 2.5;
hisVar = "2.5";
if (myVar === hisVar)
{
alert("The values of the two identifiers are identical.");
}
else
{
alert("The values of the two identifiers are not identical.");
}
</script>
The Strict Does-Not-Equal Operator
The Strict Does-Not-Equal Operator is !==. It is the opposite of the Strict Equals Operator. Here, if the two values are not equal or not of the same type, true is returned; if the two values are equal but not of the same type, true is returned; if the two values are not equal but of the same type, true is returned; otherwise, false is returned. Read and try the following:
<script type="text/ECMAScript">
myVar = 2.5;
hisVar = "2.5";
if (myVar !== hisVar)
{
alert("The values of the two identifiers are not identical.");
}
</script>
The Greater Than operator is, > . It returns true if the left operand is greater than the right operand. In the following example, the left operand is greater than the right operand. So the if-block is executed:
<script type="text/ECMAScript">
var1 = 60;
var2 = 70;
if (var2 > var1)
{
alert("The value of var2 is greater than the value of var1.");
}
</script>
Read and try the above code.
Greater Than Or Equal Operator
The Greater Than or Equal operator is, >= (it is the math greater than sign followed by the math equal sign). It returns true if the left operand is greater than or equal to the right operand.
The Less Than Operator
The Less Than Operator is < .It returns true if the left operand is less than the right operand.
The Less Than or Equal Operator
The Less than or Equal operator is, <= . It returns true if the left operand is less than or equal to the right operand.
That is it for equality and relational operators. Let us take a break here. Rendezvous in the next part of the series.
Chrys
Related Links
ECMAScript BasicsECMAScript Operators
Expressions in ECMAScript
Statements in ECMAScript
Custom Objects in ECMAScript
Functions in ECMAScript
ECMAScript Date Object
The ECMAScript String Object
ECMAScript String Regular Expressions
ECMAScript Template Literal
The ECMAScript Array
ECMAScript Sets and Maps
ECMAScript Number
Scopes in ECMAScript
Mastering the ECMAScript (JavaScript) eval Function
Sending Email with ECMAScript
ECMAScript Insecurities and Prevention
Advanced Course
Advanced ECMAScript Regular Expressions
Promise in ECMAScript 2015
Generator in ECMAScript 2015
ECMAScript Module
More Related Links
Node Mailsend
EMySQL API
Node.js Web Development Course
Major in Website Design
Low Level Programming - Writing ECMAScript Module
ECMAScript Course
BACK NEXT