Skip to main content

JavaScript Type System

JavaScript typeof

Written by Published

The typeof operator reports what kind of value you are holding.

It returns a string naming the type.

It has two famous quirks that are worth learning early.

Example

Example

javascript

console.log(typeof "hello");
console.log(typeof 42);
console.log(typeof true);

The output is string, number, boolean.

What typeof Returns

One of: string, number, boolean, undefined, object, function, symbol or bigint.

It always returns a string, never the type itself.

It is safe to use on a variable that was never declared.

Syntax

Syntax

javascript

typeof value

No brackets are needed, though they are allowed.

The null Quirk

typeof null is object, which is a bug from the very first version of JavaScript.

It was never fixed because too much code depends on it.

Example

Example

javascript

console.log(typeof null);
console.log(null === null);

To check for null, compare against it directly.

Arrays Are Objects

typeof cannot tell an array from an object.

Array.isArray is the right tool.

Example

Example

javascript

console.log(typeof []);
console.log(Array.isArray([]));
console.log(Array.isArray({}));

The output is object, true, false.

Functions Are Special

Functions are objects, but typeof gives them their own answer.

Example

Example

javascript

function greet() {}

console.log(typeof greet);
console.log(typeof class Thing {});

A class reports as function, because that is what it is underneath.

Safe on Undeclared Names

Reading an undeclared variable throws, but typeof does not.

This makes it useful for feature checks.

Example

Example

javascript

console.log(typeof somethingNeverDeclared);

The output is undefined, with no error.

Checking Before Using

This is the everyday use: make sure something is what you expect.

Example

Example

javascript

function shout(value) {
  if (typeof value !== "string") {
    return "not a string";
  }
  return value.toUpperCase();
}

console.log(shout("hi"));
console.log(shout(42));

The guard stops the method call failing on a number.

Complete Example

Complete Example

html

<!DOCTYPE html>
<html>
<head>
  <title>JavaScript typeof</title>
</head>
<body>

  <h1>typeof</h1>

  <p id="out"></p>

  <script>
    const values = ["hello", 42, true, undefined, null, [], {}, function () {}];

    const lines = values.map(function (value) {
      return JSON.stringify(value === undefined ? "undefined" : value) +
        " is " + typeof value;
    });

    document.getElementById("out").innerHTML = lines.join("<br>");
  </script>

</body>
</html>

Try It Yourself

Run the above example in the Try It Editor.

Try adding a number as text:

Add "42" to the list and see it report string, not number.

Important Points

  • typeof returns a string naming the type.
  • typeof null is object, which is a long-standing bug.
  • Arrays report as object; use Array.isArray.
  • Functions get their own answer.
  • It is safe on variables that were never declared.

Conclusion

typeof is the first tool for finding out what you are holding.

Its two quirks - null and arrays - catch out almost everyone once.

Knowing them makes type checking straightforward.