Compare Value With Data Type –
As compare with data type not only compare with the value itself but also compare with data type also. Like if you compare “5” and 5, they look equal but both types are not equal.
“5” === 5 => Not Equal
As the “5” data type is String and the other one 5 data type is Number. That’s why both of them not equal.
First, declare arrow function with name compare using const keyword and it accept one parameter.
Inside the function, parameter compare with itself with data type match.
const compare = b => b === b;
Here b is the function parameter which is passed in function and in return this function return the result of comparison.
Finally, we test this function with 4 different value which are as follows.
Now, compare with NaN, which is Not a Number.
Now, any guess what should be the output of all above statements.
Now, you know the answer is true, true, false, true. But why all value is true except the one which NaN value. Let me explain.
First, we compare the null with data type as it return true because null is nothing but internal pointer with zero. So it is like comparing two references having value zero.
And the same case happen with undefined also.
compare(null); = true
compare(undefined); = true
Compare Not A Number (NaN) –
Now, in the next step, we compare a NaN and NaN in the array. So, NaN when compare with NaN is not equal that’s why it not return true.
For more detail about NaN is not equal to NaN!
But for NaN in array is true because during comparison it implicit converted to string.
compare(NaN); = false
compare([NaN]); = true
At last, That’s why we get an answer “true, true, false, true“.
Let me know in the comment section, what you think about it.
I hope you understand the concept and logic behind it.
But wait, i have something more for you –
- JS Quiz – can string split method will be limited
- JS Quiz – can array from method be like a map method
And last but not the least, don’t forget to like, comment and share. It gives us a morale boost to remain to continue. Also, join our Facebook Page
If you have any questions, please feel free to ask me in the comment section and also let me know if you have any suggestions. As suggestions are always welcome.