Primitive types and typeof: the seven primitives and the null surprise
JavaScript has seven primitive types plus objects. Learn what typeof returns for each, why typeof null is 'object', and how primitives get methods.
Comes up in: JavaScript, Recruiter screen
Learn#
Omar’s form validator says if (typeof value === "object") readFields(value). One day a field is null, and the app crashes with “Cannot read properties of null”. typeof null is "object". That is a bug from JavaScript’s first version, kept forever so old websites keep working.
JavaScript values are either objects or one of seven primitive types: string, number, bigint, boolean, undefined, symbol and null. typeof reports them all — with that one wrong answer.
Functions are objects too, but typeof gives them their own answer, "function". Arrays are plain "object".
const values = ["hi", 42, 10n, true, undefined, Symbol("id"), null, {}, [], () => {}];
console.log(values.map((v) => typeof v).join(" "));
const name = "asha";
console.log(name.toUpperCase(), name, name.length);
try { name.nickname = "A"; } catch (e) { console.log(e.name); }Output (verified)
string number bigint boolean undefined symbol object object object function ASHA asha 4 TypeError
How does a primitive have methods like toUpperCase? When you use a property, JavaScript wraps the value in a temporary wrapper object (String, Number, …), finds the method there, then drops the wrapper. That is boxing.
Primitives cannot change. toUpperCase returns a new string; name stays "asha". Adding a property fails — in strict code, with a TypeError.
value
- 'asha' (string)
temporary wrapper
empty
name is the primitive string 'asha'. A primitive has no properties of its own.
Read the steps as text
- name is the primitive string 'asha'. A primitive has no properties of its own.
- name.toUpperCase(): JavaScript wraps the string in a temporary String object to find the method.
- The method returns a brand-new string. The wrapper is thrown away.
Predict#
Predict first
typeof [] === typeof null — true or false?
Both give "object". typeof cannot tell arrays, null or plain objects apart. Use Array.isArray(x) for arrays and x === null for null.
Interview angle#
Say it like this — about 30 seconds
“There are seven primitives: string, number, bigint, boolean, undefined, symbol and null. Everything else is an object, including arrays and functions.
typeofreturns the type name, buttypeof nullis ‘object’ — a bug from the first engine, where null used the same type tag as objects, kept for compatibility. Functions get ‘function’. Primitives are immutable; methods come from temporary wrapper objects.”
Follow-up Why is typeof null 'object'?
null was the null pointer, also 0, so it was reported as an object. Fixing it would break existing sites.Follow-up How do you check for an array or for null reliably?
Array.isArray(x) and x === null. For a general tag, Object.prototype.toString.call(x) gives “[object Array]”, “[object Date]” and so on.Follow-up Should you use new String('x')?
typeof is ‘object’ and new String('a') === 'a' is false. Use plain literals.Practice#
On your own
typeof is not precise enough for Omar’s validator. Write typeOf(v) that names nulls, arrays, dates, regexps, maps and sets correctly.
Done when
typeOf(null)is"null",typeOf([])is"array",typeOf(new Date())is"date".- Primitives and functions keep their
typeofnames; plain objects are"object".
Remember#
Try to answer each card before you open it. Then add them to your reviews.
Name the seven primitive types.
string, number, bigint, boolean, undefined, symbol, null.
What is typeof null, and why?
'object' — a bug from the first engine, kept for compatibility.
How can a primitive string have methods?
It is temporarily wrapped in a String object (boxing).
What does this print?console.log(typeof function () {}, typeof [], typeof 1n);
console.log(typeof function () {}, typeof [], typeof 1n);function object bigint
Go deeper
Four layers. Stop at the one your interview needs; each ends with a quick check.
The core ideaEvery round
Seven primitives, everything else an object. typeof is right except for null. Primitives never change; operations return new values.
Check yourself: Is a function a primitive?
No. It is an object, though typeof reports 'function'.
How it works underneathSenior rounds
The spec’s typeof table maps each type to a string, with null mapped to “object” Source: ECMA-262: The typeof operator. Property access on a primitive uses ToObject to get a wrapper for the lookup Source: MDN: typeof null.
Check yourself: Which spec operation turns a primitive into a wrapper object?
ToObject.
Edge cases and trapsWhere people slip
typeof document.all is “undefined” — a special case kept for very old sites. typeof undeclaredName is “undefined” without throwing, but a let in its TDZ throws (JS.02.02). typeof class {} is “function”.
Check yourself: What is typeof document.all?
'undefined' — a deliberate legacy exception.
In productionStaff-level answers
In validators, check value === null before typeof value === "object", or use value == null to catch null and undefined together. Prefer runtime schemas (such as Zod) at API boundaries, and TypeScript types inside (TS.01).
Check yourself: Which check catches both null and undefined?
value == null (loose equality).
Finished the five steps? Mark it, and its cards join your reviews.
Last checked 2026-10-31. Spot a mistake? Use “Report an error” below — fixes are listed on the corrections page.
Interview questions this answers
- What are the primitive types in JavaScript?
- Why is
typeof null"object"?