You will identify JavaScript primitive data types, understand that they are immutable values, and use typeof to distinguish them from objects.
What it is
Primitive data types are the simplest values in JavaScript. They are stored directly and cannot be changed in place. The primitive types are string, number, bigint, boolean, null, undefined, and symbol. When you “change” a primitive, JavaScript creates a new value and reassigns the variable. Related terms include value type, reference type, object, and wrapper object.
Why it matters
- Primitives are the building blocks for text, numbers, flags, and missing values.
- They are compared by value, so two equal primitives behave predictably.
- Understanding immutability prevents bugs when passing values to functions.
typeofhelps debug unexpected data shapes.- Knowing the
nullexception avoids incorrect type checks.
Syntax or steps
Declare a primitive with a literal value. Use const when the binding should not be reassigned, and let when it may change. Inspect the type with typeof.
const name = "Ada";
const age = 36;
const active = true;
const missing = undefined;
const empty = null;
const id = Symbol("id");
const big = 10n;
console.log(typeof name);
console.log(typeof age);
console.log(typeof active);
console.log(typeof missing);
console.log(typeof empty);
console.log(typeof id);
console.log(typeof big);
Example
let s = "str", n = 1, b = true, u = undefined;
console.log(typeof s); // "string"
console.log(typeof n); // "number"
console.log(typeof b); // "boolean"
console.log(typeof u); // "undefined"
let greeting = "Hello";
greeting = greeting + " world";
console.log(greeting); // "Hello world"
console.log(typeof null); // "object"
This example starts with four primitive variables: s is a string, n is a number, b is a boolean, and u is undefined. The typeof operator reports their primitive types. The greeting line shows immutability: the original string is not modified; a new string is created and assigned to greeting. The final line shows a special case: null is a primitive, but typeof null returns "object" because of an early JavaScript bug that was kept for compatibility.
Common mistakes
- Confusing
nullandundefined. Usenullfor an intentional empty value andundefinedfor a missing value. Check withvalue === nullorvalue === undefined. - Assuming
typeof null === "object"meansnullis an object. Treatnullas a primitive and test it separately. - Trying to mutate a primitive. Methods like
toUpperCase()return a new string; assign the result if you need it. - Using
new String()ornew Number()accidentally. These create objects, not primitives. Prefer literals such as"text"and42.
When to use it
Use primitives for simple values. Use objects or other reference types when you need mutable collections, functions, or structured data.
| Kind | Best for | Behavior |
|---|---|---|
| Primitive | Text, numbers, flags, unique IDs, missing values | Immutable, compared by value |
| Object/reference | Arrays, plain objects, functions, class instances | Mutable, compared by reference |
Practice
Guided exercise: Create variables for a product name, price, in-stock flag, and a missing description. Log each type.
const product = "Keyboard";
const price = 49.99;
const inStock = true;
const description = undefined;
console.log(typeof product);
console.log(typeof price);
console.log(typeof inStock);
console.log(typeof description);
Expected output: "string", "number", "boolean", "undefined".
Challenge: Write a function that returns true for primitives and false for objects and functions.
function isPrimitive(value) {
return value === null || (typeof value !== "object" && typeof value !== "function");
}
Solution hint: remember that null is a primitive even though typeof null is "object".
Quick check
What does typeof null return, and is null a primitive?
It returns "object", but null is still a primitive type.
Summary
JavaScript primitives are simple, immutable values such as strings, numbers, booleans, null, undefined, symbols, and bigints. They are compared by value and are best for straightforward data. The main exception to remember is that typeof null returns "object" for historical reasons.