Skip to content
BasicsChapter 5 / 47Beginner5 min read

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.

TypeExampleUsed for
string"Lahore"Text
number42, 3.14Both integers and decimals
bigint9007199254740993nVery large integers
booleantrue, falseYes or no
undefinedundefinedA value that was never given
nullnullDeliberately "no value"
symbolSymbol("id")A unique key
object{}, [], functions, DateA 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 typesUnderstanding types
Mixing text and numbers gives strange resultsCheck the type first, then operate
Treating null and undefined as the sameUsing each one knowing it means something different
Changing the original by thinking you made a copyKnowing about references, copying when needed
Bugs show up at runtimeChecking early with typeof

3. Where is it used?

Real useType
User's name, emailstring
Price, quantity, ratingnumber
Logged in or notboolean
A large database IDbigint
User profile, productobject
List of productsArray, which is also an object
An optional field that is emptynull

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.

Value vs reference
let a = 10, let b = a
b changes, a stays the same (primitive copy)
const x = object, const y = x
y changes, x changes too (same reference)

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

js
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:

js
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?

ItemMeaning
typeof nullGives "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 = aA copy of the primitive, a separate value
copy = userA 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

Final quick revision

PointDetail
Total types8 (7 primitives + object)
Primitivesstring, number, bigint, boolean, undefined, null, symbol
typeof null"object", an old bug
Primitive copyThe value is copied
Object copyThe reference is copied