Data Types
The full list of JavaScript data types, the 7 primitives and objects, the typeof operator, and the difference between value and reference. A beginner guide.
On this page
1. What are data types?
JavaScript has 8 data types. Seven are primitives and one is object.
| Type | Example | Used for |
|---|---|---|
string | "Lahore" | Text |
number | 42, 3.14 | Both integers and decimals |
bigint | 9007199254740993n | Very large integers |
boolean | true, false | Yes or no |
undefined | undefined | A value that was never given |
null | null | Deliberately "no value" |
symbol | Symbol("id") | A unique key |
object | {}, [], functions, Date | A collection of data |
2. Why does it matter?
JavaScript is dynamically typed. A variable's type is decided by its value and can change. If you do not understand types, results like "5" + 1 will surprise you.
| Not understanding types | Understanding types |
|---|---|
| Mixing text and numbers gives strange results | Check the type first, then operate |
| Treating null and undefined as the same | Using each one knowing it means something different |
| Changing the original by thinking you made a copy | Knowing about references, copying when needed |
| Bugs show up at runtime | Checking early with typeof |
3. Where is it used?
| Real use | Type |
|---|---|
| User's name, email | string |
| Price, quantity, rating | number |
| Logged in or not | boolean |
| A large database ID | bigint |
| User profile, product | object |
| List of products | Array, which is also an object |
| An optional field that is empty | null |
4. How does it work?
Primitives are copied as a value. Objects are copied as a reference, which means both variables point to the same object.
Primitives are immutable. You cannot change a character inside a string, a new string is created instead. To find out a value's type, use the typeof operator.
5. Code example
console.log(typeof "hello");
console.log(typeof 42);
console.log(typeof 10n);
console.log(typeof true);
console.log(typeof undefined);
console.log(typeof Symbol("id"));
console.log(typeof { a: 1 });
console.log(typeof []);
console.log(typeof null);
console.log(typeof function () {});Output: string, number, bigint, boolean, undefined, symbol, object, object, object, function.
The difference between value and reference:
let a = 10;
let b = a;
b = 20;
console.log(a);
const user = { name: "Hina" };
const copy = user;
copy.name = "Zara";
console.log(user.name);
const realCopy = structuredClone(user);
realCopy.name = "Nida";
console.log(user.name);Output: 10, Zara, Zara.
How does the code work?
| Item | Meaning |
|---|---|
typeof null | Gives "object", an old JavaScript bug |
typeof [] | "object", use Array.isArray() to check for an array |
typeof function | "function", but a function is also an object |
b = a | A copy of the primitive, a separate value |
copy = user | A reference to the same object |
structuredClone() | Makes a deep copy of the object |
Interview tip
"What is the difference between null and undefined?" undefined means a value has not been set yet (JavaScript gives it by itself). null is something a developer sets on purpose to say "there is no value here". typeof null returning "object" is a historical bug.
6. Common mistakes
Mistake: Checking for null or an array with typeof
Why? typeof gives 'object' for both, so the check passes wrongly.
Better: Use value === null for null and Array.isArray(value) for arrays.
Mistake: Thinking = copies an object
Why? Only the reference is copied, so a change in one place shows up in both.
Better: Use spread syntax for a shallow copy and structuredClone for a deep copy.
Mistake: Using number for very large integers
Why? number can hold integers exactly only up to 2 to the power 53, and precision is lost after that.
Better: Use BigInt for very large IDs or counts, like 9007199254740993n.
7. What to learn before and after
| Stage | Topics |
|---|---|
| Before | Variables |
| After this | Type Conversion, Strings & Template Literals, Numbers & Math |
| Further on | Objects, Arrays, Spread & Rest |
Final quick revision
| Point | Detail |
|---|---|
| Total types | 8 (7 primitives + object) |
| Primitives | string, number, bigint, boolean, undefined, null, symbol |
typeof null | "object", an old bug |
| Primitive copy | The value is copied |
| Object copy | The reference is copied |