Data Types in JavaScript
- JavaScript has 7 primitive types and 1 object type. Arrays and functions are objects.
- typeof null === 'object' is a historic bug — always use val === null to check for null.
- undefined means 'not yet set'; null means 'intentionally empty'. Use === to distinguish them.
JavaScript has 8 data types: 7 primitives (string, number, boolean, null, undefined, Symbol, BigInt) and 1 object type (which includes arrays, functions, and plain objects). Use typeof to check a type at runtime — but know its quirks: typeof null returns 'object' (a historic bug) and typeof function returns 'function'.
The 8 JavaScript Types
// Primitives — immutable values const str = 'TheCodeForge' // string const num = 42 // number (no separate int/float) const float = 3.14 // also number const bool = true // boolean const nothing = null // null (intentional absence) let undef // undefined (not yet assigned) const sym = Symbol('id') // Symbol (unique, no two are equal) const big = 9007199254740993n // BigInt (integers beyond 2^53) // Object — everything else const obj = { name: 'Alice', age: 30 } // plain object const arr = [1, 2, 3] // array (is an object) const fn = function() {} // function (is an object) const date = new Date() // Date (is an object) console.log(typeof str) // 'string' console.log(typeof num) // 'number' console.log(typeof null) // 'object' ← historic bug, not a real object console.log(typeof arr) // 'object' console.log(typeof fn) // 'function' ← special case for functions
number
object
object
function
null vs undefined
Both mean 'no value' but they mean it in different ways. undefined is the language's own 'not set yet'. null is your explicit signal that something was intentionally cleared.
// undefined — the JS engine's default for 'not assigned' let user; console.log(user); // undefined console.log(user === undefined); // true function greet(name) { console.log(name); // undefined if called with no argument } greet(); // undefined // null — your code saying 'intentionally empty' let currentUser = null; // logged out — no user // The null check gotcha console.log(null == undefined); // true — loose equality console.log(null === undefined); // false — strict equality (different types) // Best practice: always use === and check explicitly if (currentUser === null) { console.log('User logged out'); } else if (currentUser === undefined) { console.log('User state not initialised'); }
undefined
true
false
Type Coercion — Where Bugs Hide
JavaScript automatically converts types in certain contexts. This is called implicit coercion. It is the source of most JS type bugs.
// + operator: if either operand is a string, concatenates console.log(1 + '2') // '12' — number coerced to string console.log('3' - 1) // 2 — string coerced to number console.log(true + 1) // 2 — true coerced to 1 console.log(false + 1) // 1 — false coerced to 0 // == (loose) vs === (strict) console.log(0 == false) // true — coercion console.log(0 === false) // false — no coercion console.log('' == false) // true — both coerce to 0 console.log('' === false)// false // The fix: always use === for comparisons // Use explicit conversion when you mean to convert const input = '42'; // string from a form input const value = Number(input); // explicit — clear intent console.log(value + 1); // 43, not '421'
2
2
1
true
false
true
false
Checking Types Reliably
// For primitives: typeof works (except null) function typeOf(val) { if (val === null) return 'null'; // fix the null bug if (Array.isArray(val)) return 'array'; // typeof [] === 'object' return typeof val; } console.log(typeOf(null)) // 'null' console.log(typeOf([1,2,3])) // 'array' console.log(typeOf('hello')) // 'string' console.log(typeOf(42)) // 'number' console.log(typeOf({})) // 'object' // For objects: instanceof or Object.prototype.toString console.log([] instanceof Array) // true console.log({} instanceof Object) // true console.log(Object.prototype.toString.call([])) // [object Array]
array
string
number
object
true
true
[object Array]
🎯 Key Takeaways
- JavaScript has 7 primitive types and 1 object type. Arrays and functions are objects.
- typeof null === 'object' is a historic bug — always use val === null to check for null.
- undefined means 'not yet set'; null means 'intentionally empty'. Use === to distinguish them.
- Always use === instead of == — loose equality triggers coercion in confusing ways.
- Use
Array.isArray()to check for arrays — typeof returns 'object' for them.
Interview Questions on This Topic
- QWhat are the 8 data types in JavaScript?
- QWhy does typeof null return 'object' and how do you correctly check for null?
- QWhat is the difference between == and === in JavaScript?
Frequently Asked Questions
Why does typeof null return 'object'?
This is a bug in the original JavaScript implementation from 1995. null was represented internally with a type tag that matched objects. Fixing it would break existing code, so it was never corrected. Always check for null with val === null, not typeof val === 'object'.
What is the difference between Number and number in JavaScript?
Lowercase number is the primitive type — what you get from literals like 42 or 3.14. Uppercase Number is the wrapper object — rarely used directly. When you call number methods like (42).toFixed(2), JavaScript automatically wraps the primitive in a Number object temporarily. You almost never need to use new Number().
When should I use BigInt?
Use BigInt when you need integers larger than Number.MAX_SAFE_INTEGER (2^53 - 1 = 9,007,199,254,740,991). Common cases: database IDs from systems using 64-bit integers, cryptography, precise financial calculations. BigInt and Number cannot be mixed in arithmetic — convert explicitly.
Developer and founder of TheCodeForge. I built this site because I was tired of tutorials that explain what to type without explaining why it works. Every article here is written to make concepts actually click.