Chapter 13

JavaScript typeof

The typeof operator is used to check the JavaScript type of a value.

What is JavaScript typeof?

  • typeof is an operator, not a normal function.
  • It returns a string describing the type of the value.
  • typeof is useful for checking strings, numbers, Booleans, undefined values, and other values.
  • The behavior of typeof with objects has some special cases that students should learn later.

Syntax

typeof value;

Example

Try understanding this code before running it.
let name = 'Aman';
let age = 20;
let active = true;
let value;

console.log(typeof name);
console.log(typeof age);
console.log(typeof active);
console.log(typeof value);

Important Points

  • typeof 'Hello' returns 'string'.
  • typeof 25 returns 'number'.
  • typeof true returns 'boolean'.
  • typeof undefined returns 'undefined'.

Common Mistakes

  • Thinking typeof is a normal function.
  • Forgetting that typeof returns a string.
  • Assuming typeof behaves the same way for every complex data structure.

Real-World Use

typeof can help developers inspect dynamic data and choose the correct processing logic.

Practice Exercise

Practice: typeof
Create a string, number, Boolean, and undefined value. Use typeof to inspect each value.

Ready to write code?

Open the JavaScript editor and run your code with live output.

Try It Yourself →