JavaScript: JavaScript Data Types
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
string, number, boolean, null, undefined, symbol, and bigint, plus the reference type object (arrays, functions, dates, etc. all belong to object). You can use typeof to detect most types, but note that typeof null returns "object" and typeof on a function returns "function".typeof null return "object", and how do you correctly check for null?null's low bits were all 0, the same as object's identifier, so it was misclassified. Fixing it later would break a lot of existing code, so it had to stay. To check for null, use value === null directly rather than relying on typeof.null and undefined, and which should I use in practice?undefined means "not assigned" and is the default value when a declared variable is uninitialized, a function returns nothing, or a nonexistent property is accessed; null means "empty value" and must be assigned by the developer to intentionally clear something. In practice, let JS produce undefined automatically and assign null when you need to explicitly clear a reference; when checking, use === null and === undefined to avoid == conflating the two.📖 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.