Primitive checks
typeof works well for string, number, boolean, bigint, symbol and undefined.
typeof is a quick type inspection tool, but it has quirks: null reports object, arrays report object and functions report function.
typeof "User A"; // "string" typeof 12; // "number" typeof null; // "object" Array.isArray([]); // true
Values & Types
The typeof operator returns a string describing the broad type category of a value. It is fast, common and useful for primitives.
There are famous edge cases. typeof null returns "object". Arrays also return "object", so use Array.isArray for arrays. Functions return "function".
The right pattern is to use typeof for broad checks, then use more specific checks where needed: value === null, Array.isArray(value), instanceof Date or custom validation.
typeof works well for string, number, boolean, bigint, symbol and undefined.
Use value === null because typeof null returns "object".
Use Array.isArray(value), not typeof value === "array".
Callable functions return "function", even though functions are objects too.
Examples
function describe(value) {
if (value === null) return "null";
if (Array.isArray(value)) return "array";
return typeof value;
}
console.log(describe([])); // "array"
console.log(describe(null)); // "null"
typeof null === "object"; typeof [] === "object"; typeof new Date() === "object"; // These are all true, but they do not answer the same question.
Rules that matter
JavaScript lets values move flexibly, but professional code keeps meaning clear. Convert at boundaries, validate input and choose values that represent the real idea.
It is strongest for string, number, boolean, bigint, symbol and undefined.
typeof null is a historical quirk. Use value === null.
Arrays are objects, but your code often needs to know if a value is specifically an array.
"object" can mean object literal, array, date, regex or null.
typeof value + "x" and typeof (value + "x") are different questions.
typeof is only a first gate for API responses and form data.
Production thinking
typeof is the quickest flashlight for runtime values, but a flashlight is not a map. You still need the right check for the job.
Validation code often starts with type checks. Wrong type checks can show the wrong error message or no error at all.
Production input validation should combine typeof with stricter shape checks for objects and arrays.
Bad type guards can break rendering when data from APIs does not have the expected shape.
Live code lab
The preview runs inside an isolated iframe. The JavaScript is placed inside the HTML editor for now, so every example stays together and remains easy to understand.
Mini assignment
Practice assignment
Try it yourself
Self-check
Say the answer in plain language. If you can explain the value behavior, the code becomes much easier to debug.