# Data Types & Primitives

> The building blocks of JavaScript. Learn the seven primitive types and the one reference type that governs everything else.

Subject: JavaScript | Lesson 3 of 63 | 6 min read
Interactive lesson: https://codewithanimation.in/javascript/data-types-and-primitives
Previous: [Variables & Scope](https://codewithanimation.in/javascript/variables-and-scope.md) | Next: [Operators & Type Coercion](https://codewithanimation.in/javascript/operators-and-coercion.md)

## What are data types?

JavaScript has two main categories of data: Primitives and Objects (Reference types).

There are seven primitive types: `string`, `number`, `bigint`, `boolean`, `undefined`, `symbol`, and `null`.

Everything else (arrays, functions, dates, etc.) is structurally an `object`.

## Why does it matter?

Primitives are immutable - they cannot be changed once created. If you reassign a primitive variable, you are pointing it to a completely new value in memory.

Objects are mutable and passed by reference. Understanding this distinction is the key to preventing unexpected side effects when passing data between functions.

## How it works

You can use the `typeof` operator to check the type of a given value. It returns a string indicating the type of the unevaluated operand.

Primitives compare by value. Two strings containing 'hello' are strictly equal (`'hello' === 'hello'`).

Objects compare by reference. Two empty objects are not equal (`{} === {}` is false) because they occupy different locations in memory.

## Common mistake: The typeof null bug

Running `typeof null` returns `'object'`. This is a famous legacy bug in JavaScript from its earliest days. `null` is absolutely a primitive value, not an object, but fixing the `typeof` bug now would break too much existing web code.

## Tip: Checking for arrays

Because arrays are just objects under the hood, `typeof []` will return `'object'`. To reliably check if a value is an array, use the built-in `Array.isArray(value)` method instead.

## Try it: Try it out

Select a value to evaluate it. Notice the difference between the actual type and the result of the `typeof` operator.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/javascript/data-types-and-primitives

## Check yourself

### Question 1

What does `typeof null` return?

- A. 'object'
- B. 'null'
- C. 'undefined'

Correct answer: A

Due to a legacy bug in the original JavaScript engine, typeof null returns 'object'. It has never been fixed to avoid breaking the web.

- Why not B: Incorrect. While null is a primitive type, the typeof operator famously misreports it as an object.
- Why not C: Incorrect. undefined is a distinct primitive type with its own typeof result.

### Question 2

Which of the following is NOT a primitive data type?

- A. Array
- B. Symbol
- C. BigInt

Correct answer: A

Arrays are not primitives; they are a specialized type of Object (a reference type).

- Why not B: Incorrect. Symbol is a primitive introduced in ES6 for creating unique identifiers.
- Why not C: Incorrect. BigInt is a primitive used for arbitrarily large integers.

### Question 3

What is the result of `"foo" === "foo"` compared to `{} === {}`?

- A. true / false
- B. true / true
- C. false / false

Correct answer: A

Primitives compare by their actual value, so identical strings are equal. Objects compare by memory reference, so two distinct objects are never equal, even if they look the same.

- Why not B: Incorrect. Objects are compared by reference, so two distinct objects are never equal.
- Why not C: Incorrect. Primitives with the exact same value are strictly equal.

## Remember this

- JavaScript has 7 primitives: string, number, bigint, boolean, undefined, symbol, null.
- Primitives are passed by value and are immutable.
- Objects (including Arrays and Functions) are passed by reference and are mutable.
- `typeof null` famously evaluates to `'object'` due to an ancient bug.
