Skip to the workspace
Pebellslearn by building
Data Types  ›  Lesson

The typeof Operator

typeof operator 12 min

javascript / The typeof Operator

Explore each scene at your own pace.

01 · The typeof Operator

The typeof Operator

The typeof Operator

typeof is an operator that produces a string describing the broad type of a value. It does not fully identify every kind of object.

Ask JavaScript what kind of value it sees before deciding how to handle it.

See the mechanism

Walk through the modelThe typeof OperatorAuthored snapshots of the example; use Run below to test the code.
value"4"
The characters are stored as a string.
1 of 3

Follow one example

javascript

The first two outputs differ because quoted text and a numeric value have different types. typeof null is "object", a historical quirk; it does not mean null is an object you can access. Test null directly when that distinction matters.

Predict, change and check

Predict: Will typeof "false" report boolean or string? Why?

Test your explanation: Add console.log(typeof "false"). Then remove the quotes and compare. Do not use typeof alone to detect null.

The tasks below ask you to explain that result and apply the idea to different inputs.

Apply and distinguish

Use typeof for its actual granularity

typeof distinguishes broad categories, not every useful application type. Arrays, dates and null all need more specific checks than the string 'object'. Array.isArray is appropriate for an array boundary; typeof value === 'object' must also exclude null when a non-null object is required.

typeof an absent identifier can return 'undefined' without reading a property on it, but a lexical binding in its temporal dead zone can still throw. Do not use a typeof test alone as complete schema validation. For example, a string title can still violate an application's empty-title policy, and a number can be NaN or infinite.

Check your understanding: State the input contract, identify the operation that changes the result, and give one boundary input that would distinguish the correct rule from the mistake described above.

Scene 1 of 2
Project context and further resources
Additional resources
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.