How to Use the typeof Operator in JavaScript: Full Guide

The typeof operator tells you what type a value is in JavaScript. Learn how to use it, what it returns for each type, and the few gotchas to watch out for.

5 min read

typeof is a built-in JavaScript operator that returns a string telling you what type a value is. You write it before any value or expression, and JavaScript gives you back a short type label.

javascriptjavascript
console.log(typeof "hello"); // "string"
console.log(typeof 42);      // "number"
console.log(typeof true);    // "boolean"

It is the fastest way to check what kind of data you are dealing with before you try to use type-specific methods on it.

The typeof Return Values

Every value in JavaScript produces one of these typeof results:

Valuetypeof Result
a string like "hello"string
a number like 42, 3.14, NaN, or Infinitynumber
true or falseboolean
undefinedundefined
nullobject (legacy bug)
a plain object like {}object
an array like [1, 2, 3]object
a functionfunction
a Symbolsymbol
a BigInt like 9007199254740991nbigint

Notice three important surprises in this table: null returns object, arrays return object, and NaN returns number. The null case is especially confusing because null is a primitive, not an object. For a deeper comparison, see the undefined vs null guide.

Using typeof in Practice

Type guards before operations

The most common use of typeof is checking a value's type before you do something that only works with that type:

javascriptjavascript
function getLength(value) {
  if (typeof value === "string") {
    return value.length;
  }
  return 0;
}
 
console.log(getLength("hello")); // 5
console.log(getLength(123));     // 0 (not a string, safe fallback)

Without the typeof check, calling .length on a number would return undefined instead of crashing, but the intent is clearer with the guard.

Checking if a variable exists

typeof is the only operator that can safely check a variable that might not exist anywhere in your code:

javascriptjavascript
// If someLibrary was never declared, this does NOT throw an error
if (typeof someLibrary !== "undefined") {
  console.log("Library is loaded");
} else {
  console.log("Library is missing");
}

Any other way of referencing someLibrary, like writing a plain if check on it directly, would throw a ReferenceError if the variable does not exist. This pattern was common before ES modules, when scripts loaded in a specific order.

The typeof null Problem

typeof null returns "object", which is wrong. null is a primitive value that represents intentional absence of any value. It is not an object.

javascriptjavascript
console.log(typeof null); // "object" -- this is a bug, not correct behavior

This bug has existed since the very first version of JavaScript and cannot be fixed without breaking existing code on the web. The workaround is simple: never use typeof to check for null. Use strict equality instead:

javascriptjavascript
const value = null;
 
// Wrong: typeof does not help
if (typeof value === "null") { /* never true */ }
 
// Right: direct comparison
if (value === null) {
  console.log("Value is null");
}

Arrays and typeof

Arrays are one of the most common values you will run typeof against, and they trip up beginners because JavaScript does not give arrays their own dedicated type. Arrays are technically objects in JavaScript, so typeof returns "object" for them:

javascriptjavascript
console.log(typeof [1, 2, 3]);        // "object"
console.log(typeof { name: "Alice" }); // "object"

You cannot tell an array from a plain object using typeof alone, since both return the same result. Use Array.isArray() instead, which checks the internal array structure rather than the general object type:

javascriptjavascript
const items = [1, 2, 3];
const user = { name: "Alice" };
 
console.log(Array.isArray(items)); // true
console.log(Array.isArray(user));  // false

Array.isArray() is reliable and works even when arrays come from another JavaScript context, like an iframe.

Functions and typeof

typeof on a function returns "function", even though functions are technically objects:

javascriptjavascript
function greet() {
  return "Hello";
}
 
console.log(typeof greet); // "function"

This is a useful exception. There is no separate function type in JavaScript's core type system, but typeof gives you a practical way to check if something is callable:

javascriptjavascript
function safeCall(maybeFunction) {
  if (typeof maybeFunction === "function") {
    return maybeFunction();
  }
  return "Not a function";
}
 
console.log(safeCall(() => "Hi")); // "Hi"
console.log(safeCall(42));         // "Not a function"

typeof and NaN

NaN is one of the strangest values in JavaScript, and typeof adds one more surprise on top of it. NaN stands for Not a Number, but typeof NaN returns "number":

javascriptjavascript
console.log(typeof NaN); // "number"

This is technically correct because NaN is part of the IEEE 754 number specification that JavaScript follows. The value represents an invalid numeric operation result, but it still lives inside the number type. Use Number.isNaN() to check for NaN specifically:

javascriptjavascript
console.log(Number.isNaN(NaN));       // true
console.log(Number.isNaN("hello"));   // false

For more on NaN, see the NaN guide.

A Reliable Type-Checking Pattern

For everyday type checks, typeof is the right tool. Combine it with special-case checks for null and arrays when you need to handle all types:

javascriptjavascript
function getType(value) {
  if (value === null) return "null";
  if (Array.isArray(value)) return "array";
  return typeof value;
}
 
console.log(getType("hello"));  // "string"
console.log(getType(42));       // "number"
console.log(getType(null));     // "null"
console.log(getType([1, 2]));   // "array"
console.log(getType(() => {})); // "function"

This pattern gives you accurate type labels for every value you will encounter in day-to-day JavaScript.

Rune AI

Rune AI

Key Insights

  • typeof returns a string indicating the type of a value.
  • typeof null returns 'object' -- use value === null instead.
  • typeof on arrays returns 'object' -- use Array.isArray() for arrays.
  • typeof on functions returns 'function'.
  • typeof is safe to use on undeclared variables (returns 'undefined' without an error).
  • Use typeof for type guards before performing type-specific operations.
RunePowered by Rune AI

Frequently Asked Questions

Why does typeof null return 'object'?

This is a well-known bug in JavaScript that dates back to its first implementation. The internal type tag for null was set to 0, the same as objects, and the typeof check has never been fixed to preserve backward compatibility. To check for null, use value === null instead.

How do I check if a value is an array?

typeof returns 'object' for arrays, which is not helpful. Use Array.isArray(value) instead. It returns true only for actual arrays.

Does typeof work on undeclared variables?

Yes. typeof anUndeclaredVariable returns 'undefined' without throwing a ReferenceError. This is the only operator that can safely check for a variable that might not exist.

Conclusion

typeof is JavaScript's built-in type checker. It returns a string like 'string', 'number', or 'object' that tells you what kind of value you are working with. Remember that typeof null is 'object' (a legacy bug), typeof on arrays returns 'object', and typeof on functions returns 'function'. Use typeof for quick type guards, but pair it with Array.isArray for arrays and strict equality for null checks.