JavaScript Types and Values: Primitives, Objects and the typeof Operator
The big picture of JavaScript's type system: the seven primitive types and objects, what typeof returns for each (and why typeof null is "object"), dynamic typing, immutability, why primitives have methods, how garbage collection decides what to free, and how to check types reliably.
kvi5hnuCode Sprout team member7 min readOn this page
Every value a JavaScript program works with, whether it's a number, a piece of text, a list or a function, has a type. The type decides what you can do with the value: you can multiply numbers, call functions and read the characters of a string. This article gives you the complete map of JavaScript's types. The articles that follow zoom in on each one.
Primitives and objects
JavaScript divides its values into two families:

The primitive types are the simple, built-in kinds of value. There are exactly seven:
| Type | Example values | Covered in |
|---|---|---|
number | 42, -3.5, NaN, Infinity | articles 10 to 12 |
bigint | 9007199254740993n | article 13 |
string | "hello", 'A', `x` | articles 14 to 16 |
boolean | true, false | article 17 |
undefined | undefined (the only value of its type) | article 18 |
null | null (the only value of its type) | article 18 |
symbol | Symbol("id") | article 19 |
Everything that isn't a primitive is an object: a collection of named properties. Plain objects, arrays, functions, classes, dates, regular expressions, maps, sets, errors and promises are all objects. Arrays and functions are just objects with special behavior.
The difference between the two families goes deeper than syntax:
| Primitives | Objects | |
|---|---|---|
| Can the value change? | no: immutable | yes: properties can be added, changed, deleted |
=== compares | the values | the identity (is it the same object?) |
| Assigning to another variable | copies the value | copies a reference to the same object |
Article 21 explores that last row in depth, because it's the source of many bugs and interview questions.
The typeof operator
typeof returns a string naming a value's type:
const samples = [
["undefined", undefined],
["null", null],
["true", true],
["42", 42],
["42n", 42n],
['"hi"', "hi"],
['Symbol("id")', Symbol("id")],
["{}", {}],
["[]", []],
["function () {}", function () {}],
["class {}", class {}],
["new Date(0)", new Date(0)],
["/x/", /x/],
["new Map()", new Map()],
];
for (const [code, value] of samples) {
console.log(code.padEnd(15), typeof value);
}
undefined undefined
null object
true boolean
42 number
42n bigint
"hi" string
Symbol("id") symbol
{} object
[] object
function () {} function
class {} function
new Date(0) object
/x/ object
new Map() object
Two results need explaining:
typeof nullis"object". This is a bug from the very first version of JavaScript. Values were stored with a type tag, objects had tag 0, andnullwas represented as the null pointer, which also had tag 0. Fixing it was proposed, but it would break too much existing code, so it stays forever.nullis a primitive.typeofa function is"function". Functions are objects, buttypeofgives them their own answer because "can I call this?" is such a useful question. Classes report"function"too, because a class is a special kind of function.
typeof can't tell objects apart: arrays, dates and maps are all "object". Use these instead:
const list = [1, 2], when = new Date(0), lookup = new Map();
console.log(Array.isArray(list)); // the reliable array test
console.log(when instanceof Date); // is it a Date (or a subclass)?
console.log(lookup instanceof Map);
console.log(Object.prototype.toString.call(when)); // the "class tag"
console.log(Object.prototype.toString.call(list)); // works for any built-in
console.log(Array.isArray({ length: 2 })); // array-like objects are not arrays
true
true
true
[object Date]
[object Array]
false
To check for null, compare directly: value === null. To check for "an actual object", write value !== null && typeof value === "object". Forgetting the null part is a classic bug.
Dynamic typing
In JavaScript, variables don't have types; values do. A variable can hold a number now and a string later, and nothing checks the type of an argument before a function runs:
let reading = 21.5;
console.log(typeof reading);
reading = "21.5 °C"; // the same variable, now a string
console.log(typeof reading);
function double(x) {
return x * 2;
}
console.log(double(4), double("4"), double("four"));
number
string
8 8 NaN
double("4") worked because * converts strings to numbers, and double("four") produced NaN instead of an error. This flexibility makes small scripts quick to write, but in large programs type mistakes surface late and far from their cause. That's the main reason TypeScript exists: it adds static type checking on top of JavaScript (part 17).
JavaScript converts between types automatically in many situations. Articles 22 to 24 cover the rules, which every JavaScript developer needs to know.
Primitives are immutable
You can't change a primitive value; you can only replace it. String methods don't modify the string, they return a new one:
const word = "cat";
const shout = word.toUpperCase();
console.log(word, shout); // the original is unchanged
const letters = "dog";
letters[0] = "f"; // silently ignored (a TypeError in strict mode)
console.log(letters);
cat CAT
dog
Objects, by contrast, are mutable:
const pet = { name: "Rex", age: 3 };
pet.age = 4; // change a property
pet.vaccinated = true; // add one
console.log(pet);
{ name: 'Rex', age: 4, vaccinated: true }
Why do primitives have methods?
If only objects have properties, how does "cat".toUpperCase() work? When you access a property on a string, number, boolean, bigint or symbol, JavaScript temporarily wraps the primitive in an object of the matching wrapper type (String, Number, Boolean, BigInt, Symbol). That object provides the method, and it's discarded afterward. Engines optimize this away, so it costs nothing in practice.
null and undefined have no wrapper type. Accessing any property on them throws a TypeError, the source of the famous "Cannot read properties of undefined" error.
You can create wrapper objects explicitly with new String("x") or new Number(5), but don't: they're objects, so typeof says "object", they're always truthy (even new Boolean(false)), and === compares them by identity. Article 22 shows the traps.
Memory and garbage collection
JavaScript manages memory automatically. You create values freely and never free them yourself. The engine's garbage collector periodically finds values the program can no longer reach and reclaims their memory.
"Reachable" means the program can still get to the value, starting from the roots: global variables, the variables of functions currently running, and similar. A value referred to by a reachable value is reachable too. Anything else is garbage:
let user = { name: "Ana", profile: { theme: "dark" } };
let theme = user.profile; // a second reference to the profile object
user = null; // the user object is now unreachable: it can be collected
// the profile object is still reachable through theme
Garbage collection doesn't mean memory leaks are impossible. If you keep a reference you no longer need, such as an ever-growing cache, a forgotten event listener or a timer that captures large data, the value stays reachable and is never freed. Parts 11 and 15 cover WeakMap, WeakRef and the tools for finding leaks.
Common questions
Is everything in JavaScript an object?
No, although you'll often hear it. Primitives are not objects: they have no identity, can't be changed and can't hold new properties. They behave like objects when you call methods on them, because of the temporary wrapper objects.
Are arrays a separate type?
Not as far as typeof is concerned: an array is an object whose property names are indexes, with a special length property. Use Array.isArray(value) to detect arrays. It works even for arrays created in other frames or windows, where instanceof Array can fail.
How many types does JavaScript have?
Eight: seven primitive types (undefined, null, boolean, number, bigint, string, symbol) plus object. "Function" isn't a separate type in the specification; functions are callable objects.
Does typeof return capitalized names like "Number"?
No, always lowercase: "number", "string", and so on. The capitalized names (Number, String) are the wrapper constructors, a different thing.
Summary
- Values are either one of seven primitive types (
number,bigint,string,boolean,undefined,null,symbol) or objects. - Primitives are immutable and compared by value; objects are mutable and compared by identity.
typeofnames the type, with two quirks:typeof nullis"object", and functions (and classes) give"function". UseArray.isArray,instanceofand=== nullfor finer checks.- Variables are untyped; JavaScript converts between types automatically, which is flexible but error-prone.
- Primitives get methods through temporary wrapper objects.
nullandundefinedhave none, so property access on them throws. - Memory is garbage-collected based on reachability. Unneeded references still cause leaks.
Practice Questions
What does typeof typeof null return?
Answer
"string". typeof null is "object", a string, and typeof of any string is "string".
Write a function isPlainObjectLike(v) that returns true for objects (including arrays) but false for null and functions.
Answer
const isPlainObjectLike = v => typeof v === "object" && v !== null;
typeof returns "function" for functions, so they're excluded automatically, and the !== null check handles the typeof null quirk.
What prints, and why?
const s = "abc";
s.extra = 1;
console.log(s.extra);
Answer
undefined (in sloppy mode; in strict mode the assignment throws a TypeError). The property was set on a temporary wrapper object, which was discarded immediately. The primitive string itself can't hold properties.
After this code runs, which of these values can the garbage collector reclaim?
let a = { x: { y: 1 } };
let b = a.x;
a = null;
Answer
Only the outer object that a referred to. The inner object { y: 1 } is still referenced by b, so it stays reachable.
What do typeof NaN and typeof Infinity return?
Answer
Both are "number". They're special values of the number type, covered in article 11.
Previous: JavaScript Semicolons: Automatic Semicolon Insertion Rules and Traps
Next: JavaScript Numbers: Integer and Floating-Point Literals and the Math Object

More in JavaScript Course
Quiz: Modern JavaScript, ES2022 to ES2027 Features
Twenty-four questions on what's new in JavaScript: change-array-by-copy methods, groupBy, Set methods, iterator helpers, Promise.withResolvers and Promise.try, RegExp.escape and the v flag, using declarations, Error.isError, Base64 typed arrays, Temporal, getOrInsert and Iterator.zip.
Quiz: JavaScript's Delicate Details (Spec-Level Edge Cases)
Twenty-eight questions on the fine print of JavaScript, drawn from the ECMAScript specification and MDN: sparse arrays, -0, number limits, JSON edge cases, Date overflow, UTF-16, property attributes, inherited accessors, comparison quirks, labels, regex state and Symbol.toPrimitive.
Quiz: JavaScript Interview Questions (Output Prediction and Core Concepts)
Thirty classic JavaScript interview questions: closures in loops, hoisting and the TDZ, this, the event loop and microtasks, async/await, coercion, sorting and parseInt traps, prototypes, copying, finally, generators and bind. Every answer was verified by running it.
Quiz: JavaScript Course Final Exam (All 17 Parts)
The final exam for the JavaScript course: thirty questions spanning all seventeen parts, from syntax, types and operators through objects, arrays, functions, classes, modules, the standard library, iterators, async code, metaprogramming, browsers, Node.js and tooling.
Discussion
Sign in to ask a question or share what you learned.