- JavaScript
- JavaScript Fundamentals for Frontend Interviews
- Type Coercion & Equality
JavaScript Type Coercion & Equality: Rules & Traps
Type coercion is JavaScript's automatic or implicit conversion of values between data types during operations. The binary + operator favors string concatenation if either operand is a string, arithmetic operators force numeric conversion via ToNumber, and loose equality (==) coerces operands to primitives and numbers before comparing.
Key Learning Objectives
Master the ECMAScript ToPrimitive algorithm and how hint ('string', 'number', 'default') is chosen.
Understand the distinction between binary addition (+) and subtraction (-) type coercion.
Predict the outcome of loose equality (==) vs strict equality (===) comparison chains.
Explain classic JavaScript puzzles like [] == ![] and [1, 2] + [3, 4] using specification rules.
The Interview Problem
What is the exact output printed to the console when evaluating the following statements in standard ECMAScript?
1const res1 = 1 + '2' + 3;2const res2 = 4 - '2' + '1';3const res3 = [] == ![];4const res4 = [1, 2] + [3, 4];56console.log(res1, res2, res3, res4);
Predict Console Output
Select the option that matches what this code prints to the console:
'123' '21' true '1,23,4'
6 3 false [1,2,3,4]
'123' 3 true '1,2,3,4'
'15' '21' false '10'
V8 Engine Execution Trace
Step 1 of 5 (Line 1)Evaluate 1 + '2' + 3: Left-to-right. 1 + '2' has a string operand, so 1 coerces to '1', yielding '12'. '12' + 3 coerces 3 to '3', producing '123'.
JavaScript Type Coercion & Equality Explained
The ECMAScript Coercion Rules
- Binary
+vs-:- The
+operator serves dual purposes: numeric addition and string concatenation. If either operand is an object,ToPrimitiveis invoked. If either primitive operand is a string, string concatenation occurs. - In contrast, the
-,*,/, and%operators only exist for numbers. They always convert both operands usingToNumber().
- The
- The
[] == ![]Equivalence Step-by-Step:- Step A:
![]evaluates first.[]is an object reference, hence truthy.!truebecomesfalse. - Step B: Now evaluate
[] == false. - Step C: Spec rule 7.2.14: If one operand is boolean, convert it to Number (
false->0). Now[] == 0. - Step D: Object compared to number: call
ToPrimitive([]). Array toString yields"". Now"" == 0. - Step E: String compared to number:
ToNumber("")is0. Now0 == 0, which is strictlytrue!
- Step A:
- Array Concatenation Trap:
[1, 2] + [3, 4]does not merge arrays. Both arrays coerce via.toString()to'1,2'and'3,4'. Concatenating them results in'1,23,4'.
Common Traps & Mistakes
Assuming `==` checks identity or deep equality for objects. `==` only checks reference equality unless comparing an object to a primitive.
Assuming `Boolean([])` is false. All objects, including empty arrays `[]` and empty objects `{}`, are always truthy in JavaScript.
Believing `NaN === NaN` is true. `NaN` is the only value in JavaScript that is not equal to itself.
FAANG Follow-Up Probes
Probe #1
How does `Object.is()` differ from strict equality `===` for `-0` vs `+0` and `NaN`?
Probe #2
How does `Symbol.toPrimitive` override standard coercion behavior on custom objects?
Probe #3
Why does JavaScript specify `typeof null === 'object'` and why will it never be changed?
JavaScript Type Coercion & Equality Interview Questions
Key interview inquiries, edge cases, and architectural follow-ups for Type Coercion & Equality.
Why does [] + [] equal "" while [] + {} equals "[object Object]"?
The + operator converts non-primitive operands via ToPrimitive(hint: "default"). Calling valueOf() on [] returns the array itself, so toString() is called, returning "". For [] + [], "" + "" yields "". For [] + {}, {} converts via toString() to "[object Object]", resulting in "" + "[object Object]" = "[object Object]".
How does the abstract equality comparison algorithm (==) work?
The == algorithm coerces mismatched types: if comparing a number and a string, the string converts via ToNumber; if comparing a boolean, the boolean coerces to 0 or 1; if comparing an object and a primitive, the object converts via ToPrimitive. Only null == undefined evaluates to true without numeric coercion.
What is the difference between == and === in JavaScript?
Strict equality (===) performs no type coercion; it returns true only if both operands share the exact same data type and identical value (with NaN !== NaN and +0 === -0). Loose equality (==) coerces mismatched types before comparison according to the ECMAScript Abstract Equality Comparison algorithm.
How does JavaScript convert an object to a primitive value (ToPrimitive)?
The engine checks for Symbol.toPrimitive first. If absent, it checks the conversion hint: for "string", it calls .toString() then .valueOf(); for "number" or "default", it calls .valueOf() then .toString(). The first method that returns a primitive is used; otherwise a TypeError is thrown.
Why is NaN === NaN false, and how should you reliably check for NaN?
According to IEEE 754 floating-point specification and ECMAScript rules, NaN is never equal to any value, including itself. You should reliably test for NaN using Number.isNaN(val) or Object.is(val, NaN), which do not coerce non-numeric inputs like the legacy window.isNaN() does.
Why does "5" - 2 equal 3, but "5" + 2 equal "52"?
The subtraction operator (-) is strictly an arithmetic operator, forcing both operands to numbers via ToNumber("5") = 5, yielding 5 - 2 = 3. The addition operator (+) is overloaded for both string concatenation and numeric addition; if either operand is a string, it converts the other operand to a string and concatenates.
More JavaScript interview questions for freshers
