Data Types
Every value in JavaScript has a type. Knowing the types — and the quirks of typeof — helps you avoid confusing bugs.
On this page · 5 min · Beginner
On this page
- Learning objectives
- Explanation
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Beginner
- Updated
- Oct 1, 2026
Related tutorials
Learning objectives#
- List the seven primitive types
- Distinguish primitives from objects
- Use typeof and know its two famous quirks
- Check for arrays with Array.isArray()
Explanation#
JavaScript has seven primitive types: string, number, bigint, boolean, undefined, null and symbol. Everything else — arrays, functions, dates, plain objects — is an object.
Primitives are immutable and compared by value. Objects are compared by reference: two separate objects with the same contents are not equal.
typeof returns a string naming the type, with two historical quirks: typeof null is "object", and typeof [] is "object" because arrays are objects. Use Array.isArray() to detect arrays.
Code example: Inspecting types#
console.log(typeof 'hello'); // string
console.log(typeof 42); // number
console.log(typeof 10n); // bigint
console.log(typeof true); // boolean
console.log(typeof undefined); // undefined
console.log(typeof Symbol('id')); // symbol
console.log(typeof null); // object (historical bug)
console.log(typeof []); // object (arrays are objects)
console.log(typeof function(){}); // function
console.log(Array.isArray([])); // true
string number bigint boolean undefined symbol object object function true
Step-by-step explanation#
- Each line passes a value to typeof and prints the resulting string.
- typeof null returning "object" is a bug from the first version of JavaScript that cannot be fixed without breaking the web.
- Arrays are specialised objects, so typeof reports "object".
- Functions are objects too, but typeof gives them their own "function" result.
- Array.isArray() is the reliable way to check for an array.
Real-world use case#
Validating API responses, writing utility functions that accept different inputs, and debugging “undefined is not a function” errors all require a clear picture of types.
Common mistakes#
Use value === null instead of typeof value === "object".
{ a: 1 } === { a: 1 } is false because they are two different objects.
Best practices#
- Use === and !== to avoid implicit type conversion
- Use Array.isArray() for arrays and === null for null
- Use Number.isNaN() rather than the global isNaN()
- Consider TypeScript for compile-time type checking
Remember this#
Primitives: string, number, boolean, null, undefined, bigint, symbol.
Everything else is an object (arrays, functions too).
typeof null === "object" is a famous quirk.
Practice exercise#
Your task: Write a function describe(value) that returns "array", "null" or the typeof result for anything else.
Show solution
function describe(value) {
if (Array.isArray(value)) return 'array';
if (value === null) return 'null';
return typeof value;
}
console.log(describe([1]), describe(null), describe('hi')); // array null string
Mini quiz#
What is the output of this code?
console.log(typeof []);
Want more? Practise all JavaScript MCQs · JavaScript interview questions
My notes#
Saved in this browser.