QuestenaPractice that shows what to review next
Topic lesson

Equality and coercion: follow the operator's route

About 6 min

Questions stay in the language in which they were published.

These questions ask you to predict equality, truthiness, negation, and logical expressions. Work from the operator and operand types in that order; the spelling or everyday meaning of a value is not a coercion rule.

Use a short evaluation procedure

For ===, compare types first. Different types fail without conversion; object operands pass only when both references reach one object. Same-type numbers still have two special observations: NaN does not equal itself, while the two signed zeros compare equal. For ==, identify the actual type pair and apply its defined branch. It may convert, but it does not always turn both sides into strings or always turn both into numbers.

OperationProcedure and small example
Loose equalityKeep the pair visible. A boolean in a loose comparison becomes its numeric value; a string paired with a number is then converted numerically, with an empty string becoming zero. For example, "8" == 8 becomes a comparison of equal numbers and is true. Null has a narrow pairing with undefined and does not broadly match ordinary primitive values.
Boolean conversionCheck the fixed falsy set: false, both numeric zeros, 0n, the empty string, null, undefined, and NaN. Everything else in this topic is truthy; "no" and [] therefore both pass a condition.
NegationConvert to boolean, then reverse it. One ! produces the opposite boolean; two !! operations return the original truthiness as a boolean rather than restoring the original value.
Logical ANDRead left to right. Return the first falsy operand, or the last operand if none is falsy; "go" && 12 returns 12.
Logical ORRead left to right. Return the first truthy operand, or the last operand if none is truthy; "" || "fallback" returns the second string.

The tempting wrong routes

Try it

Question 19

Which statement correctly compares loose and strict equality for both 0 and the empty string against false?

  1. Only the empty string is loosely equal to false
  2. Both are loosely equal but not strictly equal to false
  3. Both are strictly equal but not loosely equal to false
  4. Only zero is strictly equal to false
Question 23

If value is the empty string, what is the result of !!value?

  1. false
  2. true
  3. the empty string
  4. undefined
Question 24

What does the expression 0 || "ready" evaluate to?

  1. 0
  2. ready
  3. true
  4. false
Start drillPractice this topic
Equality and coercion: follow the operator's route · Questena