Data Types
JavaScript categorises every value into a data type. Knowing the types determines which operations are valid, how values compare, and how memory is managed. JS has 8 built-in types — 7 primitives and 1 object type.
Two Categories: Primitive and Reference
JavaScript types fall into two categories that behave very differently in memory:
The 7 Primitive Types
These are the most basic building blocks. Each primitive holds a single, immutable value.
true or
false. The foundation of all conditional logic.
null deliberately to indicate "no value".n to create one.// string
const name = "Alice";
const greeting = `Hello, ${name}!`;
// number
const age = 25;
const price = 9.99;
const badMath = 0 / 0; // NaN
// boolean
const isLoggedIn = true;
const hasPaid = false;
// null — intentional empty value
let selectedItem = null;
// undefined — declared but not yet assigned
let score; // undefined
// symbol — unique key
const id1 = Symbol("id");
const id2 = Symbol("id");
console.log(id1 === id2); // false — always unique!
// bigint
const huge = 9007199254740991n;
Reference Types — Object, Array, Function
All non-primitive values in JavaScript are objects. Arrays and functions are special types of objects — but they're used so frequently they deserve their own treatment.
length property.// Object — key: value pairs
const user = {
name: "Alice",
age: 25,
isAdmin: false,
};
console.log(user.name); // "Alice"
console.log(user["age"]); // 25
// Array — ordered list
const colours = ["red", "green", "blue"];
console.log(colours[0]); // "red"
console.log(colours.length); // 3
// Function — callable object
function add(a, b) { return a + b; }
const multiply = (a, b) => a * b; // arrow function
console.log(add(2, 3)); // 5
console.log(multiply(4, 5)); // 20
The typeof Operator
The typeof operator returns a string
describing the type of any value. It's the primary tool for runtime type checking in JavaScript.
| Expression | typeof result | Notes |
|---|---|---|
| typeof "hello" | "string" | Any string value |
| typeof 42 | "number" | Integers, floats, Infinity, NaN |
| typeof true | "boolean" | true or false |
| typeof undefined | "undefined" | Undeclared or unassigned variables |
| typeof null | "object" | ⚠ Historic bug! null is NOT an object |
| typeof Symbol() | "symbol" | Every Symbol is unique |
| typeof 42n | "bigint" | BigInt values (n suffix) |
| typeof {} | "object" | Plain objects and arrays |
| typeof [] | "object" | Arrays also return "object" |
| typeof function(){} | "function" | Functions get their own typeof result |
console.log(typeof "hello"); // "string"
console.log(typeof 42); // "number"
console.log(typeof true); // "boolean"
console.log(typeof undefined); // "undefined"
console.log(typeof null); // "object" ← historic bug!
console.log(typeof []); // "object" ← arrays too
console.log(typeof {}); // "object"
console.log(typeof function(){}); // "function"
// Checking for null properly
const val = null;
if (val === null) {
console.log("It is null"); // use strict equality, not typeof
}
// Check if an array (typeof returns "object" for arrays)
console.log(Array.isArray([])); // true — correct way
Dynamic Typing
JavaScript is dynamically typed — variables have no fixed type. The same variable can hold a string, then a number, then an object. Types are checked at runtime, not compile time.
let x: string = "hello" — and
catches type errors at compile time. Many large projects prefer TypeScript for this reason.
Primitive vs Reference — Memory Behaviour
This is one of the most important distinctions in JavaScript. Primitives and reference types behave very differently when you copy them.
// Primitives — copied by VALUE
let a = "Alice";
let b = a; // b gets a NEW COPY of the value
b = "Bob";
console.log(a); // "Alice" — unchanged
console.log(b); // "Bob"
// Objects — copied by REFERENCE
const obj1 = { score: 100 };
const obj2 = obj1; // obj2 points to the SAME object
obj2.score = 999;
console.log(obj1.score); // 999 — obj1 was also changed!
// To create a true independent copy of an object:
const obj3 = { ...obj1 }; // spread operator (shallow copy)
const obj4 = JSON.parse(JSON.stringify(obj1)); // deep copy
Try It Yourself
Run the code below to explore all 8 data types and see typeof results live. Edit and experiment!
What does typeof null return in JavaScript?
- JavaScript has 8 data types: 7 primitives (string, number, boolean, null, undefined, symbol, bigint) + 1 object type.
- Primitives are immutable and stored by value. Reference types (object, array, function) are stored by reference.
- Use typeof to check a value's type at runtime — but note:
typeof null === "object"is a historic bug. - To check for arrays use Array.isArray(). To check for null use strict
equality:
val === null. - JavaScript is dynamically typed — variables can hold any type and change type at runtime.
- Copying an object/array copies the reference, not the value. Modifying one affects all references to it.
Rate Us on Google
Join Our Learning Community
Connect with thousands of learners, get daily tips, ask questions and stay updated with new tutorials.