Handbooks / JavaScript / Chapter 1
Types and Coercion
7 pages · ~5 min✓ Reviewed
Start here. Next up: Scope and Closures.
Primitives vs Reference Types
1. The Two Families
- 7 primitives: string, number, bigint, boolean, undefined, null, symbol
- Everything else is an object: arrays, functions, dates, maps
2. Copy vs Share
- Primitives are immutable;
let b = ais an independent copy - Objects are stored by reference:
b.x = 1changesa.xtoo s[0]='X'is ignored in sloppy mode, TypeError in strict (modules, classes)
javascript
let a = 1, b = a; b = 2; const o = {x: 0}, p = o; p.x = 1; console.log(a, o.x, [1] === [1]);
output
1 1 false
3. Compare and Mistakes
| Primitives | Objects | |
|---|---|---|
let b = a | independent copy | one shared object |
=== | by value: 'a'==='a' true | by identity: {}==={} false |
const o = {}; o.x = 1works:constfreezes the binding only{...o}is shallow, nested objects stay shared; usestructuredClone(o)
- Interview: why is
[1]===[1]false? Two separate objects;===compares identity. - Does
constmake an object immutable? No, only the binding. - Shallow vs deep copy? Spread copies one level;
structuredClonecopies nested too.
typeof Quirks
1. What typeof returns
typeofalways returns a string, never a type object- Primitives:
'string''number''bigint''boolean''undefined''symbol' - The other two:
'object'and'function'
2. The traps
| Value | typeof | Right test |
|---|---|---|
null | 'object' (legacy bug) | x === null |
[] |
vs =
1. Strict vs Loose
===compares type and value, no conversion==converts operands first (loose equality)- Default to
=== null == undefinedis true; nothing else equals them- So
x == nulltests both in one go
2. Loose Rules and Surprises
- String vs numberstring → number: '5'==5
- Boolean→ number first: true==1
- Object vs primitiveToPrimitive: [1]==1
Truthy, Falsy and Coercion Rules
1. Truthy and Falsy
- Exactly 8 falsy:
false0-00n''nullundefinedNaN - Everything else is truthy
- Surprise truthy:
'0''false'[]{}function(){}new Boolean(false)-1Infinity
2. Coercion
| Value | Number() | Value | Number() |
|---|---|---|---|
'' | 0 | null | 0 |
null, undefined and NaN
1. undefined vs null
- undefined = no value assigned: uninitialised variable, missing property or argument, no
return - null = intentional empty value, set by the programmer
| undefined | null | |
|---|---|---|
typeof | 'undefined' | 'object' |
Number(x) | NaN | 0 |
JSON.stringify | property dropped | kept |
Types and Coercion Cheat Sheet
1. Types and typeof traps
- 7 primitives (string, number, bigint, boolean, undefined, null, symbol): copied by value
- Objects, arrays, functions: by reference;
===tests identity, so{}==={}is false s[0]='X'is ignored in sloppy mode, a TypeError in strict mode
| Value | typeof | Test with |
|---|---|---|
null | 'object' | x === null |
| array | 'object' | Array.isArray(x) |
| function |
Check yourself
With o = {a:{b:1}} and const c = {...o}, you run c.a.b = 2. What is o.a.b?
- 2: spread is shallow
c.aando.aare the same objectstructuredClone(o)gives a deep copy
Why is [] == ![] true?
![]isfalse, because arrays are truthy[] == false: false becomes 0,[]becomes''then 00 == 0is true
What do 0 || 5 and 0 ?? 5 give?
5and0||skips any falsy value??skips only null and undefined