In this post, we will learn Equality Operators in Javascript and how we can use it in our program. So, let’s get started.
Equality Operators in Javascript
There are two Equality Operators in Javascript.
- Double equality operator (==)
- Triple equality operator (===)
Difference between == and ===
== | === |
It is a loose equality operator | It is a strict equality operator |
It does the type coercion | No type coercion in === |
We can use not equal to symbol in this way != | We can use not equal to symbol in this way !== |
== and ===
When to use these two Equality Operators? and what are the differences between these two operators? Let’s understand.
== (Double equality operator)
Point 1
== is a loose equality operator. It means it did not check the data type when comparing two values.
if ("181" == 181 ) { console.log("Both numbers are equal"); } else { console.log("not equal"); }
Output: Both numbers are equal
In this code, I have “181” which is a string value, and 181 which is a number value. I used == to compare these two values.
So, in this case, the if statement will be executed because both values are similar but one point to be noted is that both values have different data types.
As I said it is a loose equality operator, it did not check the data type. We can not do this using the === operator.
Point 2
It does the type coercion. Consider the example given below.
Example 1
console.log("200" == 200);
Output: true
In this code, I have compared “200” a string value to 200 a number value. In the output, you can see we have true. Javascript automatically changed the type behind the scenes. So, that’s why it is now showing both values as equal.
Example 2
let val = prompt("Type any number"); console.log(typeof val); if (val == 12) { console.log("You selected 12"); } else { console.log("not matched"); }
Output: string You selected 12
In this example, I have used the prompt() method to get the dynamic input value entered by the user.
The output return by the prompt() method is a string type even If we have entered a number value. It will give us a string-type value.
So, If I enter 12 then it will be like “12” == 12. In this case, the if statement will work because Javascript automatically converts the string type to the number type.
As you can see in line number 2 of the above code, we will get the type as a string even if I enter any Number type value.
We can convert this string value to a number type using the Number() method.
let val = Number(prompt("Type any number"));
=== (Triple equality operator)
Point 1
== is a strict equality operator. It means it checks the data type when comparing two values.
if ("181" === 181) { console.log("Both numbers are equal"); } else { console.log("not equal"); }
Output: not equal
In this code, “181” is not equal to 181. As I said, it is a strict equality operator. So, It also checks the data type. In this code, both values in the if statement is equal but one is a string and the other is a number data type.
So, the else block will run.
Point 2
There is no Type coercion in the triple equality operator (===).
Example 1
console.log("200" === 200);
Output: false
We are getting false because a string can not be equal to a number. === does not change the type unlike in ==.
Example 2
let val = prompt("Type any number"); console.log(typeof val); if (val === 12) { console.log("You selected 12"); } else { console.log("not matched"); }
Output: string not matched
I have entered 12. So, it will be like “12”===12. In this case, the else statement will work because === checks for data type as well and it does not convert data type.