๐งช
Data Types & Coercion
beginnerJavaScript has a small set of primitives and automatic type conversion rules. Understanding coercion helps you predict equality checks, arithmetic, and truthiness in conditionals.
๐ฎInteractive Visualization
Type Coercion Lab
Pick values and operators to see how JavaScript converts types.
Left value
Operator
Right value
7=="7"=true
numberstringboolean
What JS does
- Number == String: string is coerced to number
- "7" โ Number("7") = 7
- Then 7 == 7 is compared
Key Points
- Seven primitives: string, number, boolean, null, undefined, symbol, bigint
- typeof null is "object" (legacy quirk)
- Loose equality (==) coerces types, strict (===) does not
- + concatenates when either operand is a string
- Other math operators convert operands to numbers
- Truthy/falsy rules drive conditionals and &&/||
๐ปCode Examples
Loose vs strict equality
'0' == 0 // true
0 == false // true
null == undefined // true
'0' === 0 // false
0 === false // false
null === undefined // falseLoose equality coerces types, strict equality does not
The plus operator
'5' + 1 // "51"
5 + '1' // "51"
true + '1' // "true1"
5 + 1 // 6
true + 1 // 2 (true -> 1)+ concatenates when a string is involved, otherwise adds numbers
Numeric operators
'5' - 1 // 4
'5' * '2' // 10
'five' - 1 // NaNMath operators other than + convert operands to numbers
Truthiness
Boolean(0) // false
Boolean('') // false
Boolean('0') // true
Boolean([]) // trueConditionals use truthiness, not strict booleans
typeof quirks
typeof null // "object"
typeof undefined // "undefined"typeof null is a long-standing JavaScript quirk
Common Mistakes
- Using == and expecting no type conversion
- Assuming empty strings are truthy
- Forgetting that null and undefined only equal each other with ==
- Assuming typeof null returns "null"
Interview Tips
- Explain the difference between == and === with examples
- Know the falsy values: false, 0, "", null, undefined, NaN
- Be ready to reason about + with strings and numbers