Primitive Data Types
Every value in JavaScript is either a primitive or an object. A primitive is a single, simple value with no properties of its own, and there are seven types of them:
| Type | Example |
|---|---|
| string | 'tea' |
| number | 42, 3.5, NaN |
| bigint | 9007199254740993n |
| boolean | true, false |
| undefined | undefined |
| symbol | Symbol('id') |
| null | null |
Everything else is an object, including arrays, functions, dates, maps and regular expressions.
The typeof operator reports the type of each primitive:
const samples = ['tea', 42, 9007199254740993n, true, undefined, Symbol('id'), null];
for (const value of samples) { console.log(typeof value);}This prints:
stringnumberbigintbooleanundefinedsymbolobjectThe last line is a long-standing quirk: typeof null gives "object" even though null is a primitive. TypeOf Operator covers this and the other results.
Primitives cannot be changed
Section titled “Primitives cannot be changed”A primitive value is immutable. Methods that seem to change a string, such as toUpperCase(), return a new string and leave the original as it was, and assigning to a character of a string does nothing:
const word = 'kettle';const upper = word.toUpperCase();
console.log(word);console.log(upper);
word[0] = 'b';console.log(word);This prints:
kettleKETTLEkettleA variable holding a primitive can still be given a different value, if it is declared with let. That replaces the value in the variable; it does not change the old value.
Primitives are compared by value
Section titled “Primitives are compared by value”Two primitives are equal when their values are equal. Two objects are equal only when they are the same object, however alike their contents, and assigning an object to a second variable gives a second name for the same object rather than a copy:
const a = 'pear';const b = 'pear';console.log(a === b);
const first = { fruit: 'pear' };const second = { fruit: 'pear' };console.log(first === second);
const alias = first;alias.fruit = 'plum';console.log(first.fruit);This prints:
truefalseplumPass By Reference covers what this means for function arguments.
Methods on primitives
Section titled “Methods on primitives”Strings, numbers, booleans, bigints and symbols have methods and properties, such as length and at() on a string. When code reads a property of a primitive, JavaScript wraps the value in a temporary object of the matching type, such as String, looks up the property there and then discards the wrapper. This is sometimes called autoboxing. Because the wrapper is thrown away, a property assigned to a primitive is lost straight away:
const title = 'garden';console.log(title.length);console.log(title.at(-1));
title.owner = 'Ana';console.log(title.owner);This prints:
6nundefinednull and undefined have no wrapper type, so reading any property of them throws a TypeError.
Avoid the wrapper constructors
Section titled “Avoid the wrapper constructors”new String(), new Number() and new Boolean() create wrapper objects explicitly. These behave like objects, not like the primitives they hold, which leads to surprises: they fail strict equality with the primitive, and a Boolean object holding false is truthy because every object is truthy. Calling the same functions without new converts a value to the primitive type, which is useful:
const literal = 'mug';const wrapped = new String('mug');
console.log(typeof literal);console.log(typeof wrapped);console.log(literal === wrapped);
const flag = new Boolean(false);console.log(flag ? 'truthy' : 'falsy');
console.log(typeof String(42));This prints:
stringobjectfalsetruthystringBigInt() and Symbol() cannot be called with new at all, and throw a TypeError if you try.
Steve Griffith covers this topic in his video Primitive Data Types.