Equality of Values Flashcards

1
Q

What is Dan’s metaphor of the masked ball?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

In JS there are three kinds of equality. What are they?

A
  • 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)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Explain same value equality.

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does this give us:

console. log(Object.is(2, 2))
console. log(Object.is({}, {}))

A
// true
// false
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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?

A
// 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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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)); // ?

A
// false
// false
// true
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does the following give us?

console. log(2 === 2)
console. log({} === {})

A
// true
// false
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What’s the difference between same value equality and strict equality?

I.e. between Object.is and ===

A

It functions the same as same value equality with two exceptions:

  1. NaN === NaN is false even though they are the same value (historical reasons SMH).
  2. -0 === 0 is true even though they are different values.

These cases both have to do with special numbers and both are uncommon.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What type of comparison does use effect dependency array use?

A

Strict equality (===)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

When does NaN occur?

A

When we do invalid math such as:

0/0

or whenever you try do calclulations with NaN like so:

NaN * 2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Will the following console log ever be called?

if (size === NaN) {
console.log(‘Something is wrong.’);
}

A

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).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What two approaches are best to check if something is NaN?

A
  1. use isNan()

2. use Object.is(NaN, a) (i.e. same value equality)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is loose equality?

i.e. ===

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly