The Types JavaScript Gives You
Every value in JavaScript has a type, and there are exactly eight of them, split into primitives and objects.
Seven primitives (string, number, boolean, undefined, null, symbol, bigint) plus object. Primitives are copied by value; objects are shared by reference. That single difference explains a large share of beginner bugs.
EXERCISE
1Eight Types, and That Is All of Them
Every value you will ever handle in JavaScript is one of eight types, so the list is genuinely short enough to learn in one sitting.
Most languages have dozens of types. has eight, and you will use five of them constantly.
The seven primitives:
"hello" // string text of any length
42 // number whole or decimal, there is only one number type
true // boolean exactly two values: true and false
undefined // undefined a box with nothing put in it yet
null // null deliberately empty, set by you
Symbol("id") // symbol a guaranteed-unique label (rare, and advanced)
9007199254740993n // bigint whole numbers too large for number (also rare)
And the eighth, which is everything else:
// object: any value that groups other values together
const user = { name: "Ada", age: 36 }; // a plain object
const list = [1, 2, 3]; // an array is an object
const when = new Date(); // a date is an object
const greet = function () {}; // a function is an object too
// So "object" is not one shape. It is the whole category
// of things built out of other values.
Notice what is missing:
// There is no separate integer and float type.
const count = 7; // number
const price = 7.99; // also number
console.log(typeof count === typeof price); // true
// There is no separate character type.
const letter = "a"; // string of length 1
console.log(letter.length); // 1
The five types you will use every day are string, number, boolean, object and undefined. Symbol and bigint exist for specific problems you will not meet for a long time, and null is a deliberate signal rather than a kind of data. Learning the short list now means you always know what any value in your program actually is.
EXERCISE
2Primitives Are Copied, Objects Are Shared
This one difference between the two categories explains more surprising bugs than any other single fact about the language.
When you assign a primitive to a new variable, JavaScript copies the value. When you assign an object, it copies only the address of the object, and both names now point at the same thing.
Primitives: two separate boxes:
let a = 10;
let b = a; // b gets a COPY of 10
b = 20;
console.log(a); // 10 <- untouched
console.log(b); // 20
Objects: one thing, two labels:
const cart1 = { total: 10 };
const cart2 = cart1; // cart2 gets the ADDRESS, not a copy
cart2.total = 20;
console.log(cart1.total); // 20 <- changed too!
console.log(cart2.total); // 20
// There is only one object. Two names are pointing at it.
The same trap with arrays, which are objects:
const original = [1, 2, 3];
const backup = original;
backup.push(4);
console.log(original); // [1, 2, 3, 4] <- the "backup" was never separate
// To get a real copy, spread it into a new array:
const realBackup = [...original];
realBackup.push(5);
console.log(original); // [1, 2, 3, 4]
console.log(realBackup); // [1, 2, 3, 4, 5]
Pro Tip: Ask yourself "is this a primitive or an object?" whenever a value changes when you did not expect it to. Strings, numbers and booleans are always safe to pass around. Objects and arrays are shared until you deliberately copy them, and
[...array]or{...object}is how you deliberately copy them.
EXERCISE
3Every Value Has a Type, Including the Empty Ones
undefined and null both mean absence, but they arrive for different reasons and that difference is worth learning early.
Two of the eight types exist to represent nothing, and beginners mix them up constantly.
undefined means JavaScript had nothing to put there:
let winner;
console.log(winner); // undefined <- declared, never assigned
const user = { name: "Ada" };
console.log(user.age); // undefined <- property does not exist
function nothing() {}
console.log(nothing()); // undefined <- function returned nothing
null means a person deliberately put nothing there:
let selectedItem = null;
// "Nothing is selected right now, and I am telling you that on purpose."
// Later, when something IS selected:
selectedItem = { id: 7, name: "Notebook" };
// And when the selection is cleared again:
selectedItem = null;
The famous quirk you will meet in week one:
console.log(typeof undefined); // "undefined" correct
console.log(typeof null); // "object" WRONG, and famously so
// This is a bug from 1995 that can never be fixed without
// breaking a large part of the web. Everyone just knows about it.
// So to check for null, compare directly:
const value = null;
console.log(value === null); // true
undefined is the language saying "nobody gave me a value". null is you saying "the value is deliberately empty". Use undefined by not assigning anything, and use null when absence is a meaningful state in your program, such as no user logged in or nothing selected yet.
