Identity operator (===) behaves similarly to the abstract equality operator (==) but type conversion is not done for an identical operator. Types of those 2 variables should be the same to be considered equal for identical operators.
(i) == (Abstract Equal Operator) – start to check equality after doing necessary type conversation and it checks only the values of 2 variables.
0 == false // Returns true and it is because false is equivalent of 0
2 == “2” // Returns true and it is because String converted into number
(ii) === (Identity Operator) – This never does the type of conversation and if 2 values are not in the same type, it will return false. Simply, If 2 operands are the same type and have the same values, then only it produces true and otherwise, it returns false.
0 === false // Returns false and it is because both operands are of different type.
2 === “2” // . Returns false.
(2) Implicit and Explicit Type Coercion
Cohesion: Process of conversion from one type to another.
(3) Should I use identity (===) or abstract (==) equality operator?
But in some cases, the double operator(==) is worth to use while working with data. Let’s take a simple example,
i) You want to concatenate 2 array by splitting with a comma –
ii) Check the equality of number get from form text input –
Even the user inputs the number, the value we get from the text input will be a string and it makes to do the additional step to convert it into a number but we can avoid it using the abstract equal operator (==).
(4)Which one has high performance (==) or (===)?
If compare 2 variables, the performance of both 2 operators is similar because they both use the same algorithm. But it changes when the types are different for both variables. Then identical operator (===) perform better than the abstract equal operator(==). It is because it does the extra step of type coercion.
If you want, you can check the performance test for both operators using the below link,
If you check those performance tests and you will realize that the performance shouldn’t play a role in deciding when to use one operator over another while you’re coding. If you don’t need to take advantage of the abstract equal operator (==), don’t use it because you might get some unexpected results.
Simply, use always identical operator (===) except when you need type cohesion.
(5) Implicit Coercion in identity (==) operator and
If the value in the
if condition is not a boolean value, then implicitly it will be converted to the boolean type. If you use
false, null, undefined, empty string, number zero, or number NaN value for conditionals, it will return false. Everything else is treated as true.
Just memorize the following table and then you’ll able to tell the result of any values in if() condition. These all possible values that will be coerced to
false and other values will give
|String||The result will be false if the argument is the empty String. It means the length of it is zero.
otherwise, the result is true.
|Number||The result will be false if the argument is –0, +0, or NaN value. Otherwise, the result will be true.|
|Boolean||The result equals the input argument and no conversion will happen here.|
Note: Just memorize the following for
Check the following examples to get understand more.
(i) ! (Not) Operation for number – It’ll return
false for all numbers except zero (0) because all numbers other than zero(0) gives the result
(ii) ! (Not) Operation for array – It will always return false for array if we use
(iv) The weird part in ! operator –
If we do this, it is evaluated as true.
The main reason is that the + operator only works on String and numbers. If use + operator with the array it converts it to an empty string. So check the detailed view as below,
Use to converts value of any type to
Now let’s take a real-world example of fetching data from the endpoint. In the following example, let assume
fetchedData gets data from the endpoint. But endpoint will return null at some point and then
fetchedData also set to
null. Now we want to check the data is received or not. So double
!! operator would be better for it.
(9) How to check if 2 arrays are equal with the identical operator (===)
If you search google and you’ll get a lot of answers from StackOverflow to check 2 arrays. But the people arguing each other over what the right way is. So isn’t it confusing? There is no right way and it depends on what you need. So I picked one of those different approaches and let you explain the pros and cons of it.
JSON.stringify()) and check using the comparison operator. (
When it doesn’t work? – The above solution would be okay if you compare two equal arrays with the same order. Check the following example and it doesn’t work with
Above 2 arrays have the same length and exact same values but in a different order. Not worked. Now see another example where the above approach doesn’t work,
If an array contains objects and values but the order of object key might be different even 2 objects have the same keys and values. It would also fail using
If you have any questions or queries, let us know in the comment section below.
Cool Gadgets on Amazon 2019 | Cool Amazon ProductsG, 0 Comments Today’s, topic about Cool Gadgets on Amazon 2019. Now, let's go and know about those gadgets in more deep. 01. The orange screwThis is one of the most clever and versatile tools you'll ever...
Is Interstellar Travel Impossible? | Interstellar TravelG, 0 Comments Today's, topic about Interstellar Travel Impossible. Let's go into deep and understand the scientific facts related to this topic. Simply, Is interstellar travel doomed to remain in the...
Smart Retail Technology in IoT – Smart Retail StoresG, 0 Comments Today's topic is about "How IOT Gives Rise to Smart Retail Technology". Simply, I want you to picture your kitchen at home. If you're like most people you can trace many of your...