At some point of my code I need to know that value is changed or not.
Something like this:
let a = {x:func1()};
let old_x = a.x;
func2(a);
if(x === old_x)
console.log(`x wasn't changed`);
else
console.log(`x is changed`);
And this code worked for some time but I encountered 'NaN' And for NaN specs says NaN !== NaN will give 'true'... well this was quite a surprise to me, and now I wonder:
- Is there a uniform way in JavaScript to compare two values are the same?
Is 'NaN' the only one special case in JavaScript and the following condition is a safe way to compare two values for equality?
x === old_x || isNaN(old_x) && isNaN(x)
Notice: For my case 'same' means
1 === 1 => true
null === null => true
undefined === null => false
undefined === undefined => true
NaN === NaN => true
'0' === 0 => false
Infinity === Infinity => true
...etc
Objects, arrays, functions and other 'reference types' are compared by reference: i.e
let a = b = {}
a === b => true
but
a === {} => false