JavaScript Course Lesson 5 of 16
The Codex / JavaScript / Data Types

Data Types

JavaScript's 7 primitive types plus objects — and the null vs undefined trap every beginner hits.

What it is, in plain English: A data type tells JavaScript what kind of value something is — a number, some text, true/false, and so on. JavaScript has 7 primitive types (string, number, bigint, boolean, undefined, null, symbol) and one complex type (object, which includes arrays and functions). The trickiest part for beginners: null and undefined both mean 'nothing' but are subtly different.

The types of values JavaScript understands

Every piece of information in JavaScript has a type — it tells JavaScript what kind of thing it's dealing with. You can't add a number to a person's name the same way you add two numbers together. Types are how JavaScript knows the difference.

The 7 primitive types

A primitive is a simple, single value. JavaScript has seven:

string
"Hello" 'world' `template`
Text. Any characters between quotes.
number
42 3.14 -7 NaN Infinity
All numbers — integers and decimals share one type.
boolean
true false
Yes or no. On or off. Used for decisions.
undefined
undefined
A variable was declared but never given a value.
null
null
Deliberately set to "nothing". A programmer's choice.
bigint
9007199254740993n
Integers too large for a regular number. The n suffix marks it.
symbol
Symbol("id")
A unique, hidden key. Advanced — you won't need this early on.

The complex type: object

Everything that isn't a primitive is an object. Objects hold collections of values. Arrays, functions, dates, and plain key-value objects are all objects.

// Plain object — key-value pairs
let person = { name: "Alice", age: 30 };

// Array — ordered list of values
let colours = ["red", "green", "blue"];

// Functions are objects too (they can have properties)
function greet() { return "Hello!"; }

console.log(typeof person);    // "object"
console.log(typeof colours);   // "object"
console.log(typeof greet);     // "function" (special sub-type shown by typeof)

Checking the type: typeof

typeof is the operator that tells you what type a value is. Put it before any expression:

console.log(typeof "hello");     // "string"
console.log(typeof 42);          // "number"
console.log(typeof true);        // "boolean"
console.log(typeof undefined);   // "undefined"
console.log(typeof { a: 1 });    // "object"
console.log(typeof [1, 2, 3]);   // "object"  ← arrays show as object
console.log(typeof function(){}); // "function"

The null / undefined trap — the most common beginner confusion

Both null and undefined mean "nothing" — but they mean it differently:

undefined
null
What it means
"I was declared but nobody gave me a value yet"
"Someone deliberately set me to nothing"
Who sets it
JavaScript (automatically)
You, the programmer
Typical use
Uninitialized variable, missing function argument, property that doesn't exist
Intentionally clearing a value, "no user selected", "no result found"
let x;                    // declared, never assigned
console.log(x);           // undefined — JS set this automatically

let selectedUser = null;  // deliberately set to "nothing"
console.log(selectedUser); // null — a programmer's explicit choice

function getUser(id) {
  // if no user found, return null — it's an intentional "no result"
  if (id === 0) return null;
  return { name: "Alice" };
}

let user = getUser(0);
console.log(user);        // null
console.log(user === null);       // true — checking for null
console.log(user === undefined);  // false
Famous JS quirk: typeof null returns "object", not "null". This is a bug from 1995 that can't be fixed because it would break the web. To check for null: use value === null, not typeof.
Try It Yourself

Official Sources

The Codex links only to official documentation and the ECMAScript specification. No third-party tutorials.