What Are the Data Types Supported by JavaScript?
JavaScript variables can hold different types of data values. Understanding these is fundamental for every developer. JavaScript has two main categories of data types:
1. Primitive Data Types (Immutable)
- String: Text data
let name = "John";
- Number: Both integers and decimals
let age = 25;
let price = 99.99;
- Boolean: true/false
let isActive = true;
- Undefined: Uninitialized variable
let x; // undefined
- Null: Intentional absence of value
let user = null;
- Symbol (ES6): Unique identifiers
let id = Symbol('unique');
- BigInt (ES2020): Large integers
let bigNum = 9007199254740991n;
2. Non-Primitive Data Types (Mutable)
- Object: Key-value pairs
let person = { name: "John", age: 30 };
- Array: Ordered lists
let colors = ["red", "green", "blue"];
- Function: Callable objects
function greet() {
console.log("Hello!");
}
Type Checking
Use typeof operator:
typeof "Hello" // "string"
typeof 42 // "number"
typeof true // "boolean"
Special Cases
typeof null // "object" (historical bug)
typeof [] // "object"
typeof function() {} // "function"
Pro Tip: For precise type checking, use:
Array.isArray([]) // true
Object.prototype.toString.call(null) // "[object Null]"