JavaScript Data Types
Learn about the 8 data types in JavaScript and how JS thinks about values.
JavaScript Data Types
Every value in JavaScript has a type. A type tells JavaScript what kind of data it is and what you can do with it.
let age = 22; // number
let name = "Ali"; // string
let isOnline = true; // booleanJavaScript has 8 data types in total. 7 are called primitive types, and 1 is object.
The 8 Data Types
| Type | Example |
|---|---|
String | "Hello" |
Number | 42, 3.14 |
Boolean | true, false |
Undefined | undefined |
Null | null |
BigInt | 900719925474099n |
Symbol | Symbol("id") |
Object | { name: "Ali" } |
The first 7 are primitives — simple, single values. Object is everything else — arrays, functions, objects.
1. String
A string is text. Wrap it in single quotes, double quotes, or backticks.
let single = 'Hello';
let double = "World";
let backtick = `Hello, World`;Backticks are special — they let you embed variables directly inside the string:
let name = "Ali";
let greeting = `Hello, ${name}!`;
console.log(greeting); // Hello, Ali!This is called a template literal and it's the modern preferred way to build strings.
2. Number
JavaScript has just one type for all numbers — integers and decimals both.
let age = 22;
let price = 9.99;
let negative = -50;
let result = 10 / 3;
console.log(result); // 3.3333333333333335Special number values
console.log(10 / 0); // Infinity
console.log(-10 / 0); // -Infinity
console.log("Ali" * 2); // NaN (Not a Number)NaN means "I tried to do math, but the result isn't a valid number." It shows up when a math operation fails.
console.log(typeof NaN); // "number" — yes, NaN is ironically of type number3. Boolean
A boolean has only two possible values — true or false. It represents a yes/no, on/off state.
let isLoggedIn = true;
let hasNotifications = false;
console.log(isLoggedIn); // true
console.log(hasNotifications); // falseBooleans are the backbone of logic in JavaScript. Every condition in an if statement comes down to true or false.
let age = 20;
console.log(age >= 18); // true
console.log(age === 30); // false4. Undefined
A variable that has been declared but not given a value is undefined.
let username;
console.log(username); // undefinedJavaScript automatically assigns undefined as the default value until you set one. It means "this exists, but has no value yet."
let score;
console.log(score); // undefined
score = 50;
console.log(score); // 505. Null
null means "intentionally empty". You set it on purpose to say "this has no value."
let selectedUser = null; // no user selected yetnull is not zero
null means no value — and that is completely different from 0. This matters more than it sounds.
Imagine you are building a weather app. You fetch today's temperature from an API:
let temperature = null; // data hasn't loaded yet
// API responds...
temperature = 0; // it's actually 0°C todayIf you used 0 as the "no data" state, you'd never know — is it 0 because the data didn't arrive, or is it genuinely 0°C outside?
null solves this cleanly:
if (temperature === null) {
console.log("Loading weather data...");
} else {
console.log(`Temperature is ${temperature}°C`); // works even when temp is 0
}0 is a real, valid temperature. null means the data simply isn't there yet.
null vs undefined — what's the difference?
This confuses almost everyone at first. Here's the clearest way to think about it:
let a; // undefined — you forgot to give it a value
let b = null; // null — you intentionally said "no value"undefined | null | |
|---|---|---|
| Set by | JavaScript automatically | You, the developer |
| Meaning | Not assigned yet | Intentionally empty |
| Type | "undefined" | "object" ← (a known JS bug) |
console.log(typeof undefined); // "undefined"
console.log(typeof null); // "object" — this is a bug from 1995, never fixedtypeof null returning "object" is a bug in JavaScript that has existed since 1995. It was never fixed because fixing it would break millions of websites. Just remember it.
6. BigInt
JavaScript's Number type can safely handle integers up to 9007199254740991. Beyond that, precision breaks.
console.log(9007199254740991 + 1); // 9007199254740992 ✅
console.log(9007199254740991 + 2); // 9007199254740992 ❌ wrongFor numbers larger than that, use BigInt — add an n at the end:
let bigNumber = 900719925474099199999n;
console.log(bigNumber + 1n); // 900719925474099200000n ✅You'll rarely need BigInt in everyday code — it's mainly used in cryptography, finance, or systems that deal with very large integers.
7. Symbol
A Symbol is a guaranteed unique value. No two symbols are ever equal, even if they have the same description.
let id1 = Symbol("id");
let id2 = Symbol("id");
console.log(id1 === id2); // false — always uniqueSymbols are mainly used as unique keys for object properties to avoid naming conflicts. You'll rarely use them as a beginner, but they become relevant in advanced patterns.
8. Object
An object is a collection of key-value pairs. It groups related data together.
let user = {
name: "Ali",
age: 22,
isLoggedIn: true
};
console.log(user.name); // Ali
console.log(user.age); // 22Arrays are also objects:
let scores = [85, 92, 78];
console.log(typeof scores); // "object"And even functions are objects in JavaScript. Objects have their own full section — this is just an introduction to the type.
Checking Types with typeof
Use the typeof operator to check what type a value is:
console.log(typeof "Ali"); // "string"
console.log(typeof 42); // "number"
console.log(typeof true); // "boolean"
console.log(typeof undefined); // "undefined"
console.log(typeof null); // "object" ← the bug
console.log(typeof 100n); // "bigint"
console.log(typeof Symbol("x")); // "symbol"
console.log(typeof {}); // "object"
console.log(typeof []); // "object"
console.log(typeof function(){}); // "function"Primitive vs Object — The Key Difference
Primitives are copied by value. Objects are copied by reference.
// Primitives — copy by value
let a = 10;
let b = a;
b = 20;
console.log(a); // 10 — a is unchanged
console.log(b); // 20// Objects — copy by reference
let user1 = { name: "Ali" };
let user2 = user1;
user2.name = "Sara";
console.log(user1.name); // "Sara" — user1 also changed!
console.log(user2.name); // "Sara"When you copy an object, both variables point to the same object in memory. Changing one changes both. This is one of the most important behaviors to understand in JavaScript.
Summary
- JavaScript has 8 data types — 7 primitive, 1 object
- String → text, Number → all numbers, Boolean → true/false
- Undefined → not assigned yet, Null → intentionally empty
- BigInt → huge integers, Symbol → unique identifiers
- Object → collections of data (including arrays and functions)
- Use
typeofto check a value's type - Primitives copy by value, objects copy by reference