FSM Full Stack Masterclass
Platform under construction
JavaScript course badge

Values & Types

Intermediate

typeof

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

typeof is useful, but it is not a complete type system.

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.

Primitive checks

typeof works well for string, number, boolean, bigint, symbol and undefined.

null check

Use value === null because typeof null returns "object".

Array check

Use Array.isArray(value), not typeof value === "array".

Function check

Callable functions return "function", even though functions are objects too.

Examples

Good type handling makes JavaScript predictable.

Specific checks after typeof

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"

Trusting typeof too much

typeof null === "object";
typeof [] === "object";
typeof new Date() === "object";

// These are all true, but they do not answer the same question.

Rules that matter

Types are not paperwork. They are behavior.

JavaScript lets values move flexibly, but professional code keeps meaning clear. Convert at boundaries, validate input and choose values that represent the real idea.

Use typeof for primitives

It is strongest for string, number, boolean, bigint, symbol and undefined.

Check null separately

typeof null is a historical quirk. Use value === null.

Check arrays with Array.isArray

Arrays are objects, but your code often needs to know if a value is specifically an array.

Do not overtrust object

"object" can mean object literal, array, date, regex or null.

Use parentheses for expressions

typeof value + "x" and typeof (value + "x") are different questions.

Validate external data more deeply

typeof is only a first gate for API responses and form data.

Production thinking

Data quality starts with value quality.

Why does this matter?

typeof is the quickest flashlight for runtime values, but a flashlight is not a map. You still need the right check for the job.

Accessibility

Validation code often starts with type checks. Wrong type checks can show the wrong error message or no error at all.

Production note

Production input validation should combine typeof with stricter shape checks for objects and arrays.

SEO note

Bad type guards can break rendering when data from APIs does not have the expected shape.

Live code lab

Change the HTML, CSS or JavaScript and run the result.

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

Try this now.

  • Add undefined, true, 10n and Symbol("id") to the values array.
  • Remove the null check and see what label appears.
  • Replace Array.isArray with typeof and explain the difference.

Practice assignment

Do this before moving to the next topic.

  1. Write a function that returns "array" for arrays.
  2. Write a function that returns "null" for null.
  3. Use typeof to reject non-number input before a calculation.

Try it yourself

Build a better type label

Live preview

Self-check

Before you continue, prove that you understand typeof.

Intermediate

Say the answer in plain language. If you can explain the value behavior, the code becomes much easier to debug.

  1. Can you explain what typeof returns?
  2. Can you explain why typeof null is surprising?
  3. Can you explain why arrays need Array.isArray?
  4. Can you name the typeof result for a function?
  5. Can you explain why external data needs more than typeof?