In this tutorial, we are going to learn about the difference between
== equals and
Double Equals (==)
If we use double equals
== to compare the two operands of different types the double equals does the coercion before the comparison.
What is Coercion?
Let’s see an example.
First, we are comparing the number
1 with the string
"1" using a double equals operator.
1 == "1" // true
In the above example, we will get an output
true because string
1 is converted into a number
Triple Equals (===)
The triple equals (
===) is used for strict equality checking it means both operands should be same
value then only it returns
true otherwise it returns
1 === "1" // false
In the above example, we are comparing Number
1 with String
"1" the value
1 is the same on both sides but the types of both operands are different so that we will get output
2 === 2 // true
Here we are comparing Number
2 with Number
2, the value and type of both operands are the same so that we will get output
"hello" === "hello" // true 10 == "10" // true 10 === "10" // false false == 0 // true false === 0 //false (boolean false === number 0)
The main difference between tripe and double equals is.
to return true.
- Double equals convert the operands to the same type before making the comparison.