JavaScript is a dynamically typed language, meaning variables do not have fixed data types. Instead, the type is determined at runtime. This flexibility makes JavaScript powerful but can also lead to unexpected behavior if not handled properly.
In this guide, we’ll cover:
✅ The seven JavaScript data types
✅ How JavaScript handles primitive vs. non-primitive types
✅ Common pitfalls and best practices
Let’s dive in! 🚀
🔹 What Are JavaScript Data Types?
JavaScript categorizes data types into two groups:
1️⃣ Primitive Types (immutable, stored by value)
2️⃣ Non-Primitive Types (mutable, stored by reference)
🟢 1. Primitive Data Types
Primitive values are immutable (cannot be changed) and are stored by value in memory.
Type | Description | Example |
---|---|---|
String | Represents text data | "Hello, JavaScript!" |
Number | Any numeric value (integer or float) | 42 , 3.14 |
Boolean | Represents true or false |
true , false |
Null | Represents an intentional absence of value | null |
Undefined | A variable that has been declared but not assigned a value | let x; |
Symbol | Introduced in ES6, used for unique identifiers | Symbol("id") |
BigInt | Used for very large integers (introduced in ES11) | 12345678901234567890n |
✅ Key Insights on Primitive Types
- Strings are immutable – once created, they cannot be changed.
null
vs.undefined
–null
is an intentional empty value, whileundefined
means “not assigned.”- Numbers in JavaScript are all floating-point (even whole numbers like
42
). Symbol
ensures uniqueness – useful for object property keys.BigInt
is required for numbers larger thanNumber.MAX_SAFE_INTEGER
(2^53 - 1
).
Example:
let message = "Hello";
message[0] = "h"; // ❌ Strings are immutable, this does nothing
let bigNumber = 9007199254740991n; // ✅ BigInt for large numbers
🔷 2. Non-Primitive Data Types (Objects)
Unlike primitive values, objects are mutable and stored by reference.
🏗️ Types of Objects in JavaScript
Type | Description | Example |
---|---|---|
Object | General container for key-value pairs | { name: "John", age: 30 } |
Array | List-like object | [1, 2, 3] |
Function | Callable object | function greet() { return "Hello"; } |
Date | Stores date and time values | new Date() |
RegExp | Regular expressions for pattern matching | /hello/g |
Example:
let person = { name: "Alice", age: 25 };
let numbers = [1, 2, 3]; // Arrays are objects too!
✅ Key Insights on Objects
- Objects are mutable – modifying one reference affects all references.
- Arrays, functions, and dates are all objects.
- Functions are special objects that can be called.
Example:
function sayHello() {
return "Hello, world!";
}
console.log(typeof sayHello); // "function" (but it's still an object!)
🆚 Primitive vs. Non-Primitive Data Types
Feature | Primitive | Non-Primitive (Objects) |
---|---|---|
Storage | Stored by value | Stored by reference |
Mutability | Immutable | Mutable |
Copy Behavior | Creates a new copy | Creates a reference |
Example | "Hello" , 42 , true |
{ name: "John" } , [1,2,3] |
Example:
// Primitive (stored by value)
let a = 10;
let b = a; // Copy of value
b = 20;
console.log(a); // 10 (unchanged)
// Object (stored by reference)
let obj1 = { value: 10 };
let obj2 = obj1; // Reference to the same object
obj2.value = 20;
console.log(obj1.value); // 20 (changed)
🎯 Common JavaScript Type Pitfalls
❌ 1. typeof null
returns "object"
console.log(typeof null); // "object" (historical JavaScript bug)
✔️ Best Practice: Always check for null
explicitly:
if (variable === null) {
console.log("Value is null");
}
❌ 2. typeof NaN
returns "number"
NaN
stands for “Not-a-Number”, but it is actually of type "number"
.
console.log(typeof NaN); // "number"
console.log(NaN === NaN); // false (NaN is never equal to itself)
✔️ Best Practice: Use Number.isNaN()
to check for NaN values.
console.log(Number.isNaN(NaN)); // true
❌ 3. Arrays are actually objects
Even though arrays look different, they are special types of objects.
console.log(typeof [1, 2, 3]); // "object"
console.log(Array.isArray([1, 2, 3])); // true
✔️ Best Practice: Use Array.isArray()
to check if a variable is an array.
🔥 Summary & Key Takeaways
✅ JavaScript has seven data types – six primitives (String
, Number
, Boolean
, Null
, Undefined
, Symbol
, BigInt
) and one non-primitive (Object
).
✅ Primitive values are immutable and stored by value, while objects are mutable and stored by reference.
✅ Functions, arrays, and even dates are objects in JavaScript.
✅ Common pitfalls include typeof null === "object"
and typeof NaN === "number"
.
✅ Use Array.isArray()
, Number.isNaN()
, and explicit null
checks to avoid type-related bugs.
📌 What’s Next?
🔹 Want to explore more? Check out the official MDN documentation:
➡️ JavaScript Data Types & Structures
💬 Have questions? Drop a comment below! 🚀