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.
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
==,===andObject.is()for mixed types,null/undefined,NaNand-0, and know thatswitchuses strict equality. - Predict
<and>when both operands are strings versus when one is a number. - Convert explicitly with
Number(), unary+,String()andBoolean(), and test forNaNwithNumber.isNaN(). - Know the
typeofresults that surprise people:null,NaN, arrays and functions. - Key terms: coercion (an implicit, automatic type conversion); explicit conversion (calling
Number(),String()orBoolean()yourself); primitive coercion (turning an object into a primitive byvalueOf()ortoString()); 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()beforetoString()
String(obj) and template literals- Detail
toString()beforevalueOf()
null == undefined- Detail
true;null == 0isfalse
Boolean in
==- Detail
- Converted to 1 or 0 first
NaN === NaN- Detail
false;Object.is(NaN, NaN)istrue
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)
- Addition (+)developer.mozilla.org (opens in a new tab)
- Subtraction (-)developer.mozilla.org (opens in a new tab)
- Numberdeveloper.mozilla.org (opens in a new tab)
- JavaScript data types and data structuresdeveloper.mozilla.org (opens in a new tab)
- Equality (==)developer.mozilla.org (opens in a new tab)
- Equality comparisons and samenessdeveloper.mozilla.org (opens in a new tab)
- switchdeveloper.mozilla.org (opens in a new tab)
- Less than (<)developer.mozilla.org (opens in a new tab)
- typeofdeveloper.mozilla.org (opens in a new tab)
- Number.isNaN()developer.mozilla.org (opens in a new tab)
- 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