Equality of Values Flashcards
What is Dan’s metaphor of the masked ball?
Dan explains object equality as a masked ball. Some of the masks look exactly the same but the people wearing them take up different space - the masks are the same but they are not referentially the same.
In JS there are three kinds of equality. What are they?
- Loose equality (Abstract Equality Comparison) represented by double equals (==).
- Strict equality (Strict Equality Comparison) represented by triple equals (===).
- Same value equality represented by Object.is(a, b)
Explain same value equality.
Same value equality:
- is checked by invoking Object.is(a, b)
- tells us if two values are the same
- Despite ‘Object’ in the method name, Object.is is not specific to objects - It can compare any two values
// Examples
console. log(Object.is(2, 2)); // true
console. log(Object.is({}, {})); // false
What does this give us:
console. log(Object.is(2, 2))
console. log(Object.is({}, {}))
// true // false
What does:
let dwarves = 7 let continents = '7' let worldWonders = 3 + 4
console. log(Object.is(dwarves, continents)) // ?
console. log(Object.is(continents, worldWonders)) // ?
console. log(Object.is(worldWonders, dwarves)) // ?
Give you?
// false // false // true
If two values are represented by a single shape on our diagram, it means that they aren’t really two different values. They are the same value! In those instances, Object.is(a, b) returns true.
What does the following give us?
let banana = {}; let cherry = banana; let chocolate = cherry; cherry = {};
console. log(Object.is(banana, cherry)); // ?
console. log(Object.is(cherry, chocolate)); // ?
console. log(Object.is(chocolate, banana)); // ?
// false // false // true
What does the following give us?
console. log(2 === 2)
console. log({} === {})
// true // false
What’s the difference between same value equality and strict equality?
I.e. between Object.is and ===
It functions the same as same value equality with two exceptions:
- NaN === NaN is false even though they are the same value (historical reasons SMH).
- -0 === 0 is true even though they are different values.
These cases both have to do with special numbers and both are uncommon.
What type of comparison does use effect dependency array use?
Strict equality (===)
When does NaN occur?
When we do invalid math such as:
0/0
or whenever you try do calclulations with NaN like so:
NaN * 2
Will the following console log ever be called?
if (size === NaN) {
console.log(‘Something is wrong.’);
}
Nope! This will never get logged: the check is always false! For historical reasons this is a bug in JS - even though NaN is a primitive it fails the strict equality checks (which sees it as evaluating to an object literal).
What two approaches are best to check if something is NaN?
- use isNan()
2. use Object.is(NaN, a) (i.e. same value equality)
What is loose equality?
i.e. ===
Loose equality, also called abstract equality, are rarely used in practice - there are just too many edgecases to be used effectively.
The rules of loose equality are generally considered bad design decisions when JS was still being built out in 10 days.