JavaScript: JavaScript Data Types
Last updated: 2026-10-13
Data types determine what operations you can perform — numbers support arithmetic, strings support concatenation, booleans support conditionals. Understanding types helps you avoid JS's most classic pitfalls.
1. JS Data Types
JavaScript has 8 data types, divided into primitives and reference types:
Primitives (7 types): String, Number, BigInt, Boolean, Undefined, Null, Symbol
Reference type (1 type): Object (arrays, functions, dates, etc. are all subtypes of Object)
Primitive data is stored in stack memory and copied by value during assignment. Reference type data is stored in heap memory and copied by reference (address) during assignment. This distinction will be covered in depth in later lessons.
2. The typeof Operator
typeof is an operator that checks data types and returns a string:
| Value | typeof Result |
|---|---|
"hello" |
"string" |
42 |
"number" |
9007199254740991n |
"bigint" |
true |
"boolean" |
undefined |
"undefined" |
null |
"object" (historical bug) |
{ } |
"object" |
function(){} |
"function" |
Note: typeof null returns "object" — this is a historical bug from JS's inception that can never be fixed (changing it would break existing code). Just remember it.
3. String and Number Pitfalls
The + operator in JS serves two purposes: numeric addition and string concatenation. When one side of + is a string, JS converts the other value to a string as well:
"5" + 3becomes"53"(string concatenation, not math)5 + 3becomes8(math addition)"5" - 3becomes2(subtraction has no concatenation semantics, so it auto-converts to numbers)
This is one of JS's most classic pitfalls — every beginner falls for it. The fix: use Number() or parseInt() for explicit conversion before math operations.
4. null vs undefined
Both mean "no value," but they differ in intent:
- undefined: a variable has been declared but not assigned, or a property doesn't exist — "no value given yet"
- null: the developer explicitly set it to empty — "intentionally set to nothing"
Think of it this way: undefined is a new house with no furniture yet; null is when you deliberately moved all the furniture out.
▶ Example: typeof with Various Types
▶ Try it Yourself▶ Example: String and Number Pitfalls
▶ Try it Yourself▶ Example: null vs undefined
▶ Try it Yourself▶ Example: All Data Types at a Glance
▶ Try it Yourself❓ FAQ
typeof null return "object"?null had all zero low bits — the same as the object identifier — so it was misclassified. They tried to fix it later, but changing it would break too much existing code, so the bug became permanent. It's like a building with a crooked foundation — too many people live there to tear it down.typeof returns "object" for both arrays and objects, so it can't distinguish them. The correct approach is Array.isArray() — for example, Array.isArray([1,2]) returns true, while Array.isArray({}) returns false.NaN (Not a Number) has typeof NaN returning "number". Even stranger, NaN === NaN returns false — it's not even equal to itself. Use isNaN() or Number.isNaN() to check for NaN.📖 Summary
- JS has 8 data types: 7 primitives + 1 reference type (Object)
- The
typeofoperator checks types, buttypeof nullreturns"object"— a historical bug +concatenates when a string is involved —"5" + 3gives"53", not8nullmeans "intentionally empty";undefinedmeans "no value yet" — they are not strictly equal- Use
Number()orparseInt()for explicit conversion before math operations to avoid implicit coercion pitfalls - Arrays
[]also return"object"from typeof — useArray.isArray()to distinguish arrays from objects
📝 Exercises
- Basic: Declare 6 variables for 6 primitive types (String, Number, Boolean, Undefined, Null, Object), use
typeofon each, and display the results on the page. - Intermediate: Create a page with an input field. When the user enters any value, JS detects its type using typeof and displays the result. Try entering numbers, strings, true, null, etc., and observe typeof's output.
- Challenge: Create a "Type Trap Quiz" page with 5 multiple-choice questions testing type coercion knowledge, such as what
"1" + 2equals, whethernull == undefinedis true or false, etc. Show correct/incorrect feedback with explanations when the user clicks an option.