Variables, Types, and Collections23% of the exam

Type coercion and equality operators

When JavaScript converts types on its own: + concatenation vs arithmetic, how objects become primitives through valueOf and toString, == vs === vs Object.is, relational comparison of strings, typeof surprises, explicit conversion with Number, String and Boolean, and the NaN checks that actually work.

Official exam objective
Given a scenario or example, demonstrate awareness of type coercion and its effects.

~6 min read · 10 practice questions ·

What the exam expects

  • Predict the result of +, -, * and += when one operand is a string, null, undefined, an array or an object.
  • Evaluate ==, === and Object.is() for mixed types, null/undefined, NaN and -0, and know that switch uses strict equality.
  • Predict < and > when both operands are strings versus when one is a number.
  • Convert explicitly with Number(), unary +, String() and Boolean(), and test for NaN with Number.isNaN().
  • Know the typeof results that surprise people: null, NaN, arrays and functions.
  • Key terms: coercion (an implicit, automatic type conversion); explicit conversion (calling Number(), String() or Boolean() yourself); primitive coercion (turning an object into a primitive by valueOf() or toString()); loose equality (==, converts types first); strict equality (===, never converts).

Key facts

'5' + 3
Detail
'53': a string operand makes + concatenate
'5' - 3
Detail
2: - always converts to numbers
null / undefined as numbers
Detail
0 / NaN
+ and == on objects
Detail
valueOf() before toString()
String(obj) and template literals
Detail
toString() before valueOf()
null == undefined
Detail
true; null == 0 is false
Boolean in ==
Detail
Converted to 1 or 0 first
NaN === NaN
Detail
false; Object.is(NaN, NaN) is true
switch matching
Detail
Strict equality
'10' < '9'
Detail
true: two strings compare by code unit
typeof null
Detail
"object"
Number.isNaN('abc')
Detail
false: no coercion
Sources for this section (10)
  • Why does `'10' < '9'` return `true` while `'10' < 9` returns `false`?
  • When does `price + ''` use an object's `valueOf()` instead of its `toString()`?
  • What does `typeof null` return?
  • Why is `Number.isNaN()` safer than `isNaN()` for validation?
  • What do `Number('')` and `Number(undefined)` return?

The questions are free to read. Answers with their sources and self-grading are included with the pack.

Keep reading with the JavaScript Developer pack

  • Core concepts
  • Exam traps
  • Worked scenario

The pack includes:

  • All 24 lessons of the JavaScript Developer guide, with reading progress
  • Interactive decision walks and step-by-step sequence steppers
  • Inline practice questions and one-click topic practice (10 questions for this lesson)
  • An official source cited for every claim
  • An AI tutor that answers from the guide
  • Unlimited timed practice exams

Practice for the JavaScript Developer exam