Operators are the symbols in between two operands.
They are essential in a programming language for performing various operations including arithmetic, logical, bitwise, conditional, and so on.
Operators in JavaScript are used to compare two values to determine if an expression is true or false.
π π‘ Points to keep in mind:
JavaScript includes operators that perform some operation on single or multiple operands (data value) and produce a result.
The Operators in JavaScript are helpful in comparing values, assigning values, performing arithmetic and logical operations, and so on.
Let's consider a very simple expression 3 * 7 = 21. Here, 3 and 7 are ''operands'' and ''*'' is an operator.
π»
JavaScript includes various categories of operators:
- Arithmetic operators,
- Comparison operators,
- Logical operators,
- Assignment operators,
- Conditional operators(Ternary operator ?:)
- Bitwise Operators
- Unary operators
- Relational operators
- Comma operator
π In this article, I will focus on comparison (relational) and equality operators β they are the two most common types of operators you will encounter in JavaScript.
Comparison Operators
There will be times in creating logic to solve problems that you will need to use comparison or relational operators to conditionally render something to the screen. Letβs take a look at the most common ones you will see in your code. Here is the object we will use for the next few operators:
in
β use the in
operator to see if there is a property in your object:
const cityData = {
city: "Berlin",
country: "Germany",
area: 891.8,
land: 891.8,
water: 3.12,
urban: 3,769,495,
metro: 6,144,600,
elevation: 34,
population: 3,769,495,
timezone: "Berlin/Europe",
website: "www.berlin.gov"
}
console.log("metro" in cityData); //true
console.log("country" in cityData); //false
instanceof
β use the instanceof
operator to ask if an object is an instance of a class or constructor function
function Class(subject, teacher, numStudents) {
this.subject = subject;
this.teacher = teacher;
this.numStudents = numStudents;
}
const classroom = new Class('JavaScript', 'Irene Doe', 26);
console.log(classroom instanceof Class);
// expected output: true
console.log(classroom instanceof Object);
// expected output: true
Less than ( < ),
Less than or equal to ( <= )
β A comparison operator that returns true in a conditional statement if operand A
is less than operand B
.
It is most commonly when we create a for loop:
for(let i = 1; i <= n; i++) {
// code here
}
for(let i = 0; i < arr.length; i++) {
// code here
}
Greater than ( > )
Greater than or equal to ( >= )
β A comparison operator that returns true in a conditional statement if operand A is greater than operand B. Itβs used quite often when we are trying to find the maximum number in an array:
let maximum = -Infinity;
for(let i = 0; i < arr.length; i++) {
if(arr[i] >= maximum) {
maximum = arr[i];
}
}
π Attention: >=
is not the same as =>
.
The latter is used for big arrow functions in ES6 JavaScript syntax.
Equality Operators π€
Similar to comparison operators, equality operators also evaluate to a Boolean value that declares whether or not an expression is true.
Equality ( == ) vs. Identity ( === )
β When we see equal signs ( = ) in JavaScript, one equal sign is an assignment operator and not what we were used to when we were in math class.
- Two equal signs is strictly an equality operator. It only checks to see if the values are equal by attempting to convert the values to the otherβs data type. This is called type coercion.
console.log(8 == '8'); //true
Similarly, if we see a bang/exclamation point along with one equal sign ( != ), known as the inequality operator, it compares two values to see if operands are not equal in number. It doesnβt check for type.
console.log(8 != '4'); //true
Conversely, the identity operator, three equal signs ( === ), checks for type and number when comparing the two values.
console.log(8 === '8'); //false
console.log(8 === 8); //true
Just like the inequality operator, the nonidentity operator ( !== ) checks to see if the operands are unequal. In addition, it checks the type as well. If they are, the condition is true and will return true. If not, it will return false.
console.log(8 !== '8'); //true
console.log(8 !== 8); //false
Summary
Comparison and equality operators are essential to constructing logic in programming.
When we compare the left operand with the right operand, we use equality operators to see if the values are equal in type, not in type, in type and number, or not in type and number.
In addition, we use the comparison operators to help with the logic that will render a user interface (UI). When you feel comfortable with these operators, check out logical operators, the ternary operator, and bitwise operators!
Happy Coding!
Top comments (4)
Just pointing out that === is not the identity operator.
The === uses the Strict Equality Comparison algorithm, as opposed to == which uses the Abstract Equality Comparison algorithm.
You can easily tell that === is not an identity operator by considering a case like this:
I'm not sure where the idea that === is an identity operator comes from, but I've seen this mistake repeated many times. :)
JavaScript is performing the equality evaluation. The interpreter first converts both operands to the same type. Then executes the identity comparison.
The identity evaluation algorithm ( ===) some rules
NaN
in identity (and in equality) operator compared with any other value always evaluates to false. Let's consider some examples. Itβs the best way to remember the strict comparison algorithm.Operands are different types (number and string) and based on rule 1 they are not identical.
Operands are the same type (number) and have the same value, so they are strictly equal based according to the rule in line 6.
Both operands are undefined and applying rule 3 itβs an equality.
Because operands are different types, based on rule 1 theyβre not identical.
Operands are the same type (numbers), but the rule 4 indicates than nothing is equal with a NaN. The result is
false
.Both variables firstObject and secondObject are references to the same object and based on rule 8 the identity operator evaluates to true.
The
[ ]
literal creates a new array reference.Both operands being the same type (object), however have reference to different objects. rule 9 says that the identity evaluates to
false
.Again, if NaN === NaN is false, === cannot be an identity operator. :)
You'll note that your rules have no "reference to" in them.
[] and [] just have different values - the value of an object is not determined by its properties.
Both are equality operators. One exists because of the failure of the second. I would say == is equality with type coercion === is equality without type coercion.