Data types describe the different kinds of data that we can work with and store in variables. In JavaScript, there are eight basic data types, which are divided into two categories: Primitive and Object (Reference) types.
JavaScript is a "dynamically typed" language. This means you don't have to declare the type of data a variable will hold, and the same variable can hold different types of data at different times.
let x; // x is currently undefined
x = 5; // x is now a number
x = "Mim"; // x is now a string
Primitive values are single pieces of data. They are immutable (cannot be changed). There are 7 primitive types in JavaScript:
Textual data wrapped in quotes.
Ex: "Hello"
Integers and decimals.
Ex: 42, 3.14
Logical values.
Ex: true, false
A variable that has not been assigned a value.
Represents an intentional "empty" or "nothing" value.
Very large integers beyond the limit of Number type.
Unique and immutable values used as object keys.
There is only one non-primitive type in JavaScript: The Object.
Objects can store collections of data and more complex entities. Common objects you will use include:
{ name: "Mim", age: 25 }[1, 2, 3, 4]new Date()When you use operators like +, JavaScript behaves differently based on the
data type:
let x = 5 + 5; // Result is 10 (Addition)
let y = "5" + 5; // Result is "55" (Concatenation)
let z = "5" + "5"; // Result is "55" (Concatenation)
typeof null is "null". It actually returns "object" — a decades-old JavaScript quirk that trips up beginners checking for null with typeof. Use value === null instead.
=== NaN. NaN === NaN is always false — NaN is the only value that isn't equal to itself. Use Number.isNaN(value) to test for it correctly.
== instead of ===. Loose equality performs type coercion, so "0" == false is true even though they're different types. Strict equality (===) avoids these surprises.
Run this in your browser console to see the well-known type quirks for yourself:
console.log(typeof null); // "object" (the famous quirk)
console.log(typeof undefined); // "undefined"
console.log(typeof NaN); // "number" (NaN is technically a Number)
console.log(NaN === NaN); // false
console.log(Number.isNaN(NaN)); // true
Q: Why does typeof null return "object"?
A: It's a bug from JavaScript's first version in 1995 that's been kept ever since for backward compatibility — fixing it now would break too much existing code on the web.
Q: What's the difference between null and undefined?
A: undefined means a variable was declared but never given a value. null is an intentional "no value" that a developer assigns on purpose.
Q: How do I check if two values are of the same type?
A: Compare typeof valueA === typeof valueB for primitives. See the MDN typeof reference for how it behaves across every data type.