Difference Between == & === in JavaScript
Last Updated :
01 Feb, 2024
In Javascript, equality operators like double equals (==) and triple equals (===) are used to compare two values. But both operators do different jobs. Double equals (==) will try to convert the values to the same data type and then try to compare them. But triple equals (===) strictly compares the value and the datatype.
Two Types
- Double equality (==)
- Triple equality (===)
Example 1: Double Equality (==)
In this example, we are trying to compare a string and a number.
- Assigning the value “10” to a variable called string.
- Assigning the value 10 to a variable called number.
- The third statement compares the string and number and prints the result on the console. Here we are using double equals (==) so it checks the inner value rather than the datatype.
Javascript
let string = "10" ;
let number = 10;
console.log(string == number);
|
The output is true, because they have different data type but same value.
Example 2: Triple Equality (===)
In this example, we are trying to compare a string and a number.
- Assigning the value “20” to a variable called string.
- Assigning the value 20 to a variable called a number.
- The third statement compares the string and number and prints the result on the console. Here we are using triple equals (===) so it checks the inner value and the datatype.
Javascript
let string = "20" ;
let number = 20;
console.log(string === num);
|
Output:
false
Share your thoughts in the comments
Please Login to comment...