Skip to content
jshacks.dev
Fundamentals

The Seven Primitive Types in JavaScript and What They Hold

JavaScript has seven primitive types, from undefined and null to bigint and symbol. See what each holds and why a primitive never changes.

7 min read
Summarize in AI
Flat illustration of an editor window full of code, with the JavaScript, TypeScript, Node.js, npm and Vite logos.

A value in JavaScript is either a primitive or an object, and nearly every confusing behavior in the language, from === surprises to "why did my copy change", traces back to which of the two you hold. There are exactly seven primitives. Learning them as a list of seven and one rule, that they never change, removes a surprising amount of guesswork. This post goes through each type, what it is for and where it surprises people, on Node.js 24 and ES2026. The wider map of the type system is in the complete guide to JavaScript types and coercion, and this post is the starting point for it.

Here are the seven, with the literal that creates each and what typeof says.

JavaScript
const samples = [
  undefined,
  null,
  true,
  42,
  "text",
  9007199254740993n,
  Symbol("id"),
];

for (const value of samples) {
  console.log(typeof value);
}
Text
undefined
object
boolean
number
string
bigint
symbol

Six names match their type, and null reports "object". That is a bug from the first version of the language that was never fixed because too much code depends on it. The post on typeof covers the history and the safe check.

What each primitive is for

undefined means no value has been assigned: a declared variable never set, a missing property, a function with no return. null is a value you assign deliberately to say "nothing here", which makes the two different in intent even though both mean absence. When each one appears is the next question most people have.

boolean is true or false. number is a 64-bit floating point value, the only numeric type for most of the language's history, which is why it holds both integers and fractions and why 0.1 + 0.2 misbehaves. bigint holds whole numbers of any size, written with an n suffix. string is a sequence of UTF-16 code units. symbol is a unique value, usually used as a property key that cannot collide with any other.

TypeExampleHoldsNotable limit
undefinedundefinedThe absence of an assigned valueNot an error, only empty
nullnullA deliberate empty valuetypeof null is "object"
booleantrueTwo valuesEverything else converts to it in conditions
number3.14A 64-bit floatIntegers are exact only up to 2^53
bigint10nAn integer of any sizeCannot be mixed with number in arithmetic
string"hi"UTF-16 textImmutable
symbolSymbol("x")A unique tokenCannot be converted to a string implicitly

Two of those limits turn into error messages, and it helps to see them once.

JavaScript
try {
  console.log(1n + 1);
} catch (error) {
  console.log(error.message);
}

try {
  console.log(Symbol("id") + "");
} catch (error) {
  console.log(error.message);
}

console.log(String(Symbol("id")));
Text
Cannot mix BigInt and other types, use explicit conversions
Cannot convert a Symbol value to a string
Symbol(id)

The language refuses to guess in both cases. Explicit conversion, String(symbol) or BigInt(1) + 1n, is allowed, and that is the pattern across the whole type system: implicit conversion where it is safe, an exception where it would hide a loss. Details are in the post on BigInt.

Primitives never change

Immutability is the property that defines the group. You cannot alter a primitive; you can only produce a different one. String methods look as if they edit, and they return a new string.

JavaScript
const word = "node";
const loud = word.toUpperCase();

console.log(word);
console.log(loud);

let count = 5;
let copy = count;
copy += 1;

console.log(count, copy);
Text
node
NODE
5 6

word is unchanged after toUpperCase(). Assigning copy = count copied the value, so incrementing copy left count alone. There is nothing to share, because a primitive has no identity apart from its content. Objects behave the other way, and the post on primitives versus objects is the other half of this idea.

Trying to change a primitive in place fails quietly in sloppy mode and loudly in strict mode. ES modules are strict, so a file run as a module shows the error.

JavaScript
const name = "abc";

try {
  name[0] = "X";
} catch (error) {
  console.log(error.message);
}

try {
  name.extra = 1;
} catch (error) {
  console.log(error.message);
}

console.log(name);
Text
Cannot assign to read only property '0' of string 'abc'
Cannot create property 'extra' on string 'abc'
abc

In a script without "use strict", both assignments do nothing and raise no error, which is why the bug survives. Every modern toolchain emits strict mode, so the error is what you will meet.

How a primitive has methods

If primitives have no properties, how does "abc".length work? The engine wraps the primitive in a temporary object of the matching type, looks the property up on that object, and discards the object. The wrapper types are String, Number, Boolean, BigInt and Symbol.

JavaScript
const text = "abc";

console.log(text.length);
console.log(typeof text);

const boxed = new String("abc");

console.log(typeof boxed);
console.log(boxed === "abc");
console.log(boxed.valueOf() === "abc");
Text
3
string
object
false
true

The explicit new String("abc") is a real object, so typeof says "object" and strict equality with the primitive fails. Nobody should write new String in application code. Calling String(value) without new converts and returns a primitive, and that is the form to use. Boxing a primitive for a method call is cheap and optimized by the engine, so there is no reason to avoid method calls on strings and numbers.

Comparing by value

Because a primitive is its content, two of them are equal when the content matches, with no concept of "the same one".

JavaScript
console.log("a" + "b" === "ab");
console.log(10 === 5 + 5);
console.log(Symbol("id") === Symbol("id"));
console.log({ id: 1 } === { id: 1 });
Text
true
true
false
false

Symbols are the one primitive where equal-looking values are different: each call to Symbol() makes a new unique value, and the description is a label for debugging only. Objects are never equal unless they are the same object, as covered in how equality works for objects and arrays.

Strings are sequences, not arrays

A string indexes like an array and reads like one, and it is not one. It has length and at() and iterates by code point, but it has no push, and each edit produces a new string. Building a large string by repeated concatenation in a loop is allowed and fast on modern engines, which use ropes internally, so the habit of collecting pieces in an array and joining is no longer required for speed. It remains good for readability when there are many parts.

The unit of length is the UTF-16 code unit, not the character a person sees, so an emoji counts as two. Strings in JavaScript are UTF-16 has the consequences.

Choosing between number and bigint

The two numeric primitives overlap on whole numbers, and the choice is practical. number is a double, so integers are exact up to 9,007,199,254,740,991, which Number.MAX_SAFE_INTEGER names. Past that, neighboring integers collapse into the same double. bigint has no such ceiling, at the price of a separate arithmetic that will not mix with number.

JavaScript
console.log(Number.MAX_SAFE_INTEGER);
console.log(9007199254740992 === 9007199254740993);
console.log(9007199254740992n === 9007199254740993n);
console.log(2n ** 70n);
Text
9007199254740991
true
false
1180591620717411303424n

The second line is the failure: as numbers, two different integers compare equal because the second cannot be represented. Use bigint for ids that exceed 2^53, as some databases and social platforms issue, and for exact integer arithmetic. Keep number for everything else, because JSON cannot carry a bigint, Math functions reject it, and mixing them throws. We ship ids over the safe limit as strings across a JSON boundary and convert at both ends. Note that console.log prints a bigint with its n suffix, which is a reminder that the value is not a plain number and will not serialize with JSON.stringify() without a custom replacer.

Where this model stops

The seven-and-one rule describes the language, not the machine. Engines store small integers, strings and object references in different ways and move values between them. You cannot observe that from JavaScript, and it is not worth designing around.

There is also one value that is neither: typeof reports "function" for callable objects, which is a fine distinction in the operator and not an eighth primitive. Functions are objects. And two proposals, records and tuples, would add immutable compound values compared by content, but they have not reached the language, so code that needs value equality for structures still compares fields or serializes them.

For a concrete next step, read the post on truthy and falsy values to see how each primitive behaves in a condition, and how type coercion works for the conversions between them.

The list is defined in the ECMAScript specification under ECMAScript language types, and MDN keeps a readable summary on the JavaScript data types and data structures page.

Taggedbeginner
Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.