Everything About JavaScript Data Types
In JavaScript, data types define the kind of value that can be stored and manipulated. JavaScript is a dynamically typed language, meaning variables can hold values of any data type, and the type can change at runtime.
JavaScript Data Types
JavaScript has two main categories of data types:
Primitive Types
Reference Types
1. Primitive Types
These are immutable (cannot be changed) and directly represent a single value.
Primitive Data Types:
Number:
Represents both integers and floating-point numbers.
Example:
let age = 25; let price = 19.99;
BigInt:
Used to represent integers larger than
Number.MAX_SAFE_INTEGER
(2^53 - 1).Example:
let largeNumber = 1234567890123456789012345678901234567890n;
String:
Represents text, enclosed in single (
'
), double ("
), or backticks (`
).Example:
let name = 'John'; let greeting = `Hello, ${name}`;
Boolean:
Represents
true
orfalse
.Example:
let isLoggedIn = true;
Undefined:
A variable that has been declared but not assigned a value.
Example:
let result; console.log(result); // undefined
Null:
Represents an intentional absence of any value.
Example:
let data = null;
Symbol:
Represents a unique, immutable value, often used as property keys.
Example:
let id = Symbol('uniqueId');
2. Reference Types (Objects)
These are mutable and can store collections of values or more complex entities.
Reference Data Types:
Object:
A collection of key-value pairs.
Example:
let user = { name: 'Alice', age: 30 };
Array:
An ordered list of values.
Example:
let colors = ['red', 'green', 'blue'];
Function:
A block of reusable code.
Example:
function greet() { console.log('Hello!'); }
Date:
Represents dates and times.
Example:
let today = new Date();
RegExp:
Represents regular expressions for pattern matching.
Example:
let pattern = /abc/;
Map, Set, WeakMap, WeakSet:
Special objects for key-value storage (
Map
) or unique values (Set
).Example:
let map = new Map(); let set = new Set();
Special Considerations
Dynamic Typing:
A variable's type can change at runtime:
let value = 42; // Number
value = 'Hello'; // String
value = true; // Boolean
Type Checking:
Use typeof
to check the type of a value:
console.log(typeof 42); // "number"
console.log(typeof 'Hello'); // "string"
console.log(typeof true); // "boolean"
console.log(typeof null); // "object" (a known quirk in JavaScript)
console.log(typeof undefined); // "undefined"
Primitive vs. Reference Types
Feature | Primitive Types | Reference Types |
Storage | Stored in the stack. | Stored in the heap. |
Mutability | Immutable (cannot be changed). | Mutable (can be modified). |
Copying | Copied by value. | Copied by reference. |
Example:
// Primitive
let a = 10;
let b = a;
b = 20;
console.log(a); // 10 (a is not affected)
// Reference
let obj1 = { name: 'Alice' };
let obj2 = obj1;
obj2.name = 'Bob';
console.log(obj1.name); // "Bob" (obj1 is affected)
Summary
JavaScript has 7 primitive types:
Number
,BigInt
,String
,Boolean
,Undefined
,Null
, andSymbol
.All other types are reference types, primarily
Object
and its variations (e.g.,Array
,Function
).Use
typeof
to check types and understand whether a value is primitive or reference.