Chapter 13
JavaScript typeof
The typeof operator is used to check the JavaScript type of a value.
01 • Learn
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.
02 • Syntax
Syntax
typeof value;
03 • Example
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);
04 • Remember
Important Points
- typeof 'Hello' returns 'string'.
- typeof 25 returns 'number'.
- typeof true returns 'boolean'.
- typeof undefined returns 'undefined'.
05 • Avoid
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.
06 • Practical
Real-World Use
typeof can help developers inspect dynamic data and choose the correct processing logic.
07 • Practice
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.