← Back to Chapters

JavaScript typeof Operator

? JavaScript typeof Operator

⚡ Quick Overview

The typeof operator in JavaScript is used to find out the type of a value, variable, or expression. It always returns the type as a string, such as "number", "string", or "boolean".

? Key Concepts

  • typeof checks the type of a value at runtime.
  • It returns a string that names the type (for example, "string" or "object").
  • Works with variables, literals, and expressions.
  • Some values have special behaviors, like null and arrays.
  • Useful in debugging, validation, and generic functions that accept multiple types.

? typeof Return Values

The typeof operator can return one of these strings:

  • "undefined"
  • "object"
  • "boolean"
  • "number"
  • "string"
  • "symbol"
  • "function"
  • "bigint"

? Syntax

There are two common ways to use typeof:

? View Syntax
typeof variableName
typeof (expression)

You can use typeof directly with a variable or wrap an expression in parentheses. In both cases, the result is a string.

? Basic Examples

? View Basic typeof Examples
console.log(typeof "Hello");          // "string"
console.log(typeof 42);               // "number"
console.log(typeof true);             // "boolean"
console.log(typeof undefined);        // "undefined"
console.log(typeof { name: "John" }); // "object"
console.log(typeof [1, 2, 3]);        // "object" (arrays are objects)
console.log(typeof null);             // "object" (historical quirk)
console.log(typeof function(){});     // "function"

? typeof with Changing Values

Here is how typeof behaves when the same variable changes type:

? View Type Change Example
let x;
console.log(typeof x);     // "undefined"

x = 100;
console.log(typeof x);     // "number"

x = "JavaScript";
console.log(typeof x);     // "string"

x = true;
console.log(typeof x);     // "boolean"

x = null;
console.log(typeof x);     // "object" (special case)

? typeof with Arrays

Arrays return "object" with typeof. To be sure a value is an array, use Array.isArray().

? View Array Type Check Example
let arr = [1, 2, 3];

console.log(typeof arr);          // "object"
console.log(Array.isArray(arr));  // true

? Live Output / Explanation

? How to Read the Console Results

  • typeof "Hello" → returns "string" because the value is text.
  • typeof 42 → returns "number" for any numeric value (integer or decimal).
  • typeof true → returns "boolean" for true or false.
  • typeof undefined → returns "undefined" when a variable has no value.
  • typeof { name: "John" } → returns "object" for plain objects.
  • typeof [1, 2, 3] → also returns "object", because arrays are a kind of object.
  • typeof null → returns "object" due to a historical bug in JavaScript.
  • typeof function() {} → returns "function" for functions.

? When to Use typeof

  • To check if a variable was initialized or is still undefined.
  • To write functions that handle different input types safely.
  • To debug unexpected values in your code.
  • To distinguish between primitives like "string", "number", and "boolean".

? Tips & Best Practices

  • typeof is great for quick debugging and runtime type checking.
  • To check for null, use a strict comparison: x === null.
  • To check if a value is an array, use Array.isArray(x) instead of typeof.
  • Remember that typeof always returns a string like "number", not the keyword number.
  • Use typeof carefully with objects; many different structures (arrays, dates, objects) all return "object".

? Try It Yourself

  • Check the type of a BigInt value, for example: typeof 123n.
  • Create a function and check its type with typeof myFunction.
  • Check the type of null and undefined in the console.
  • Declare a variable without assigning a value and see what typeof returns.
  • Use Array.isArray() along with typeof to compare the results for arrays.
  • Open the browser console and try typeof on different values you use in your own code.