JavaScript | ‘===’ vs ‘==’Comparison Operator

25 May, 2022
In Javascript(ES6), there are four ways to test equality which are listed below:
Using ‘==’ operator
Using ‘===’ operator
SameValueZero: used mainly in sets, maps and arrays.
SameValue: used elsewhere
Now, our main concern is getting to know the difference between the ‘==’ and ‘===’ operators that the javascript provides, though they look similar, yet they are very different. ‘==’ operator: In Javascript, the ‘==’ operator is also known as loose equality operator which is mainly used to compare two value on both the sides and then return true or false. This operator checks equality only after converting both the values to a common type i.e type coercion. Note: Type coercion means that the two values are compared only after attempting to convert them into the same type. Let’s look at all the value in which the ‘==’ operator will return true. Example: 
javascript
// '==' operatorconsole.log(21 == 21);console.log(21 == '21');console.log('food is love'=='food is love');console.log(true == 1);console.log(false == 0);console.log(null == undefined);
Output:  In the above code, when we compare 21 with ’21’ the javascript will convert the ’21’ into the number value of 21 and hence we get true, similar thing happens when we try to check if ‘true == 1’, in that case, the javascript basically converts 1 into a truthy value and then compares and return the boolean true. The case of (null == undefined) is special as when we compare these values we get true, but it’s not crystal clear why?. In javascript there are two types of values: true or false True
‘0’
‘false’ // false wrapped in string.
[]
{}
function(){}
False
” or “” // empty string
false
0
null
undefined
NaN // not-a-number
So both null and undefined are false values and they represent an ’empty’ value or undefined in js, hence the comparison with ‘==’ operator returns true. Now, let’s look at all the value in which the ‘==’ operator will return false. Example: 
javascript
// '==' operatorconsole.log(21 == 32);console.log(21 == '32');console.log(true == 0);console.log(null == false);
Output:  In the above code when we compare null with false we get false, as null being a primitive data type it can never be equal to a boolean value, even though they belong to the same false group. ‘===’ operator: Also known as strict equality operator, it compares both the value and the type that is why the name “strict equality”. Let’s see some code where ‘===’ operator will return true. Example: 
javascript
// '===' operatorconsole.log('hello world' === 'hello world');console.log(true === true);console.log(5 === 5);
Output:  Simply checking the types and values at both the sides and then just printing out the boolean true or false. Some example where it will return false. 
javascript
// '===' operatorconsole.log(true === 1);console.log(true === 'true');console.log(5 === '5');
Output: 
immukul
surinderdawra388
javascript-basics
javascript-operators
JavaScript
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
Remove elements from a JavaScript Array
Roadmap to Learn JavaScript For Beginners
Difference Between PUT and PATCH Request
How to Open URL in New Tab using JavaScript ?
How to get character array from string in JavaScript?
Node.js | fs.writeFileSync() Method
How do you run JavaScript script through the Terminal?
JavaScript | Promises