The typeof Operator
What you'll learn: How to use typeof to discover what type of value you're working with.
What is typeof?
Now that you know JavaScript has seven primitive types (undefined, null, boolean, number, string, symbol, and bigint), you need a way to check what type a value actually is. The typeof operator is your detective tool—it examines any value and returns a string telling you its type.
Think of typeof like a label reader at a warehouse: you hand it a box (value), and it tells you what category it belongs to.
How to Use It
The syntax is simple:
typeof value
Here are examples matching the types you've learned:
typeof undefined // "undefined"
typeof true // "boolean"
typeof 42 // "number"
typeof NaN // "number" (yes, NaN is still a number!)
typeof Infinity // "number"
typeof "hello" // "string"
typeof Symbol() // "symbol"
typeof 9007199254740991n // "bigint"
One Important Quirk
There's a famous oddity you should know:
typeof null // "object" ⚠️
This is a bug from JavaScript's earliest days that was never fixed (fixing it would break too many websites). Just remember: null is primitive, even though typeof says "object".
Why It Matters
As you write programs, you'll often receive values from unknown sources. Using typeof lets you verify what you're working with before performing operations, preventing errors.
Key Takeaway: The typeof operator returns a string describing a value's primitive type—your go-to tool for type checking at runtime.