Tutorials JavaScript Data Types
Ch 9 / 78
JavaScript Beginner Chapter 7

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.

What you'll learn in this chapter
The 7 primitive types: string, number, boolean, null, undefined, symbol, bigint
Reference types: object, array, function
Use the typeof operator
Understand dynamic typing
Primitive vs reference type memory behaviour
Avoid the null typeof quirk

Two Categories: Primitive and Reference

JavaScript types fall into two categories that behave very differently in memory:

Primitive types
Values are stored directly in the variable. There are 7: string, number, boolean, null, undefined, symbol, bigint. They are immutable — you can't change their underlying value, only reassign the variable.
Reference types
Variables store a reference (memory address) to the value — not the value itself. Includes object, array, function. Multiple variables can point to the same object.

The 7 Primitive Types

These are the most basic building blocks. Each primitive holds a single, immutable value.

string
String
A sequence of characters. Enclosed in single quotes, double quotes, or backticks (template literals).
"Hello" 'World' `Hi ${name}`
number
Number
Any numeric value — integers, floats, Infinity, -Infinity, and NaN (Not a Number). All are the same type.
42 3.14 -7 Infinity NaN
boolean
Boolean
Only two possible values — true or false. The foundation of all conditional logic.
true false
null
Null
Intentional absence of any value. You set a variable to null deliberately to indicate "no value".
null
undefined
Undefined
A variable that has been declared but not yet assigned a value. JS assigns this automatically.
undefined // also: missing obj prop
symbol
Symbol (ES6)
A guaranteed unique, immutable value. Used as unique object property keys to avoid name collisions.
Symbol("id") Symbol("id")
bigint
BigInt (ES2020)
Integers of arbitrary precision — beyond the safe integer limit of Number. Append n to create one.
9007199254740991n 42n
JS — all 7 primitives in code
// 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.

object
Object
A collection of key-value pairs. Keys are strings (or symbols); values can be any type. The most versatile structure in JS.
Array
Array
An ordered, indexed list of values. A special type of object with numeric keys and a length property.
function
Function
A callable object. Functions are first-class in JS — they can be stored in variables, passed as arguments, and returned from other functions.
JS — reference types in code
// 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
JS — typeof in practice
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.

Step 1
let x = "hello"
typeof: "string"
Step 2
x = 42
typeof: "number"
Step 3
x = true
typeof: "boolean"
TypeScript
Dynamic typing gives flexibility but can lead to bugs. TypeScript (a JS superset) adds optional static types — 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.

Primitive — copied by value
a"Alice"
b = a"Alice" (new copy)
Reference — copied by reference
obj1→ 0x1a2b (heap)
obj2 = obj1→ 0x1a2b (same!)
JS — copy by value vs copy by reference
// 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
⚡
Free Resource
Download our JavaScript Data Types Cheat Sheet
All types, typeof results, and type coercion rules on one page.
Get Sheet

Try It Yourself

Run the code below to explore all 8 data types and see typeof results live. Edit and experiment!

Live Editor
✎ HTML / JS
👁 Preview
Quick Check — Chapter 7

What does typeof null return in JavaScript?

Chapter Summary
  • 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

4.8 / 5
Enjoying this tutorial?
Your 5-star review helps other learners discover our free tutorials and encourages us to keep creating quality content.
80+ reviews
Write a Google Review
Join WhatsApp Channel