QUESTION 05 / 20 · COMPARISONS · BEGINNER

Same value, different type

What will this code print? Read each line, then make your prediction.

Where this matters

Form input often arrives as a string. Decide whether to convert it before comparing it with a numeric value.

JavaScript · read-only example
console.log(0 === '0');
console.log(0 == '0');
Your prediction

Read the full explanation

Strict equality does not coerce the different types. Loose equality converts this number/string pair before comparing.

Trace the code

  1. The first comparison has a number on the left and a string on the right. Strict equality rejects this type mismatch.
  2. The second comparison applies the loose-equality rules, which convert this string to a number.
  3. These results describe this particular pair; loose equality is not a general-purpose text parser.

Why the other predictions differ

  • true true — Strict equality distinguishes the number from the string.
  • false false — Loose equality converts string zero to numeric zero here.
Expected console output
false
true

Reading this explanation does not record mastery. A correct submitted attempt can do so, including after review.

Change one thing

Convert the string deliberately, then compare two numbers with strict equality.

Predict the result, then run this self-contained snippet in your browser’s developer console. It uses only sample values and makes no network requests.

console.log(0 === Number('0'));
Check the variation’s output
true

Explain which binding, value, or operation changed before moving to the next challenge.