Data Types

Every value in JavaScript has a type, which determines what you can do with it. There are seven primitive types for simple values like text and numbers, and one structural type — the object — for everything else, including arrays and functions.

The primitive types

TypeExamplesUsed for
string"hello", 'hi', `hey`Text
number42, 3.14, -7, NaNIntegers and decimals
booleantrue, falseYes/no values
undefinedundefined"No value has been assigned"
nullnull"Deliberately empty"
bigint12345678901234567890nVery large integers
symbolSymbol("id")Unique identifiers

Strings, numbers and booleans are by far the most common. Lessons 13–19 cover them in depth.

Checking a type with typeof

JS typeof.js
console.log(typeof "hello");
console.log(typeof 42);
console.log(typeof true);
console.log(typeof undefined);
console.log(typeof 10n);
console.log(typeof Symbol("x"));
Output
string
number
boolean
undefined
bigint
symbol

There are two famous oddities. typeof null is "object" — a bug from the very first version of JavaScript that can never be fixed without breaking old websites. And typeof a function is "function", even though functions are technically objects:

JS typeof-quirks.js
console.log(typeof null);
console.log(typeof [1, 2]);
console.log(typeof function () {});
console.log(Array.isArray([1, 2]));
Output
object
object
function
true

To check for an array, use Array.isArray(). To check for null, compare directly: value === null.

Objects

Anything that isn't a primitive is an object — a collection of named values. Arrays, functions, dates and regular expressions are all special kinds of objects:

JS objects.js
const person = { name: "Ava", age: 30 };
const colors = ["red", "green", "blue"];

console.log(person.name, person.age);
console.log(colors[0], colors.length);
Output
Ava 30
red 3

Lessons 29 and 36 cover arrays and objects in detail.

Dynamic typing

JavaScript is dynamically typed: a variable doesn't have a fixed type, only its current value does. The same variable can hold a number now and a string later:

JS dynamic.js
let data = 42;
console.log(typeof data);

data = "forty-two";
console.log(typeof data);
Output
number
string

That flexibility is convenient, but it also means type mistakes only appear when the code runs. TypeScript — the subject of its own course — adds type checking on top of JavaScript to catch them earlier.

Primitives are copied, objects are shared

This difference matters constantly. Assigning a primitive copies the value; assigning an object copies a reference to the same object:

JS copy.js
let a = 5;
let b = a;
b = 10;
console.log(a, b);

const first = { score: 5 };
const second = first;
second.score = 10;
console.log(first.score, second.score);
Output
5 10
10 10

Changing b doesn't affect a, but changing second.score also changes first.score — both names point to one object. Lesson 33 shows how to make real copies with spread syntax.

Try it yourself

Predict each typeof result before running:

Try it yourself
Console output