Type Coercion (== vs ===, truthy & falsy)

JavaScript has a habit of quietly converting values between types. It's why "5" + 1 is "51" but "5" - 1 is 4 . These "WAT" moments confuse beginners, but once you understand the rules they become completely predictable.

Learn Type Coercion (== vs ===, truthy & falsy) in our free JavaScript course — a beginner-friendly interactive lesson with runnable examples, a practice…

Part of the free JavaScript course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

This lesson demystifies coercion, the falsy values, and the single habit that prevents most equality bugs: always use === .

🌐 Real-World Analogy: Coercion is like an over-eager travel agent who auto-converts currency:

Implicit coercion happens automatically inside operations. Explicit coercion is when you convert on purpose. The same conversion, two ways:

+ means addition and string concatenation. If either operand is a string, JavaScript concatenates; otherwise it adds. Every other math operator ( - * / % ) always coerces to numbers.

(The blank line is the empty string from [] + [] .)

Any value used in a boolean context (an if , && , ! ) is coerced to true or false. There are exactly eight falsy values — everything else is truthy.

=== (strict) compares value and type with no conversion. == (loose) coerces first, which leads to genuinely bizarre results. The lesson: use === .

Look at the contradiction: 0 == "" and 0 == "0" are both true, yet "" == "0" is false. Loose equality is not transitive — another reason to avoid it.

When you control the conversion, your code is clear and bug-resistant. Here are the idiomatic patterns:

These lines should safely turn a form value into a number, defaulting to 0 for invalid input, and report whether it's positive. They are scrambled — reorder them.

Why: the raw text (B) must exist before conversion (D). Number trims the surrounding spaces and yields 42 . The guard (A) replaces NaN with 0 so bad input can't poison later math. Only then can the result be reported (C). Note the final + here is string concatenation because the left side starts with a number followed by a string literal.

123 — evaluated left to right: 1 + "2" concatenates to "12" (because one side is a string), then "12" + 3 concatenates to "123" .

Only A prints. The string "0" is non-empty, so it's truthy. The number 0 is one of the eight falsy values, so the second if is skipped.

true false — loose == has a special rule making null and undefined equal to each other. Strict === checks type too, and they are different types, so it's false .

Up next: a Checkpoint that combines iterators, strings, numbers, loops and conditionals into one build! 🏁

Practice quiz

What does '5' + 1 evaluate to?

  • 6
  • '6'
  • '51'
  • 51

Answer: '51'. The + operator concatenates when either side is a string, producing the string '51'.

What does '5' - 1 evaluate to?

  • 4
  • '4'
  • '51'
  • NaN

Answer: 4. Unlike +, the - operator always coerces operands to numbers, so '5' - 1 is 4.

What does 1 + '2' + 3 evaluate to?

  • 6
  • '15'
  • 123
  • '123'

Answer: '123'. Left to right: 1 + '2' is '12', then '12' + 3 is '123'.

How many falsy values are there in JavaScript?

  • Five
  • Eight
  • Six
  • Ten

Answer: Eight. There are exactly eight: false, 0, -0, 0n, '', null, undefined, and NaN.

Is the string '0' truthy or falsy?

  • Truthy
  • Falsy
  • It throws
  • Depends on context

Answer: Truthy. Only the empty string is falsy; any non-empty string, including '0', is truthy.

What does 0 == '' evaluate to?

  • false
  • An error
  • true
  • undefined

Answer: true. Loose equality coerces both sides to 0, so 0 == '' is true.

What does null == undefined evaluate to?

  • false
  • true
  • An error
  • NaN

Answer: true. A special loose-equality rule makes null and undefined equal to each other.

What does null === undefined evaluate to?

  • true
  • An error
  • undefined
  • false

Answer: false. Strict equality checks type too, and null and undefined are different types, so it is false.

What does NaN == NaN evaluate to?

  • true
  • false
  • NaN
  • An error

Answer: false. NaN is never equal to anything, including itself; use Number.isNaN to test for it.

What is the recommended habit to avoid loose-equality bugs?

  • Always use ==
  • Avoid comparisons entirely
  • Always use ===
  • Use Boolean() before comparing

Answer: Always use ===. The lesson stresses always using === to avoid the inconsistent coercion rules of ==.