Primitive Data Types in JavaScript
Primitive data types are simple, immutable (unchangeable) values stored directly in memory.
When you copy them into another variable, the value itself is copied, not a reference.
JavaScript has 7 primitive data types:
Number – The Numeric Type
Represents any number — whole, decimal, or even special values like Infinity
and NaN
(Not a Number).
- Math operations, counters, or prices.
const price = 9.99;
let count = 5;
console.log(price + count); // 14.99
console.log(10 / 0); // Infinity
console.log("abc" * 2); // NaN
String – Text and Characters
A sequence of characters inside quotes ('
, "
, or ```).
- Storing names, labels, or messages.
const name = "Ravi";
let greeting = `Hello, ${name}!`;
console.log(greeting); // Hello, Ravi!
console.log(name + " ji"); // Ravi ji
Boolean – True or False
Represents logical values: true
or false
.
- Conditions, toggles, and decision-making.
const isActive = true;
let hasAccess = false;
console.log(isActive && !hasAccess); // true
Undefined – Not Yet Assigned
A variable that has been declared but not assigned any value.
- To show that something hasn’t been initialized.
let x;
console.log(x); // undefined
console.log(x === undefined); // true
Null – Empty on Purpose
Represents intentional absence of value.
- Clearing or resetting a variable.
const data = null;
console.log(data === null); // true
console.log(data ?? "Empty"); // Empty
Symbol (ES6) – Unique Identifier
A unique and immutable value, often used as object keys to avoid naming conflicts.
- Adding private or hidden properties in objects.
const key = Symbol("id");
const obj = { [key]: 123 };
console.log(obj[key]); // 123
BigInt (ES2020) – For Huge Numbers
Represents integers larger than the safe limit for regular numbers (2^53 - 1
).
- Handling very large IDs, timestamps, or cryptographic data.
const big = 12345678901234567890n; // Notice the 'n'
console.log(big + 10n); // 12345678901234567900n