Javascript - The typeof Operator
Code Life
By Pierre Marsaa
What is the typeof Operator?
When you're coding, sometimes you need to check what type of data you're working with. Is it a number? A string? A boolean? JavaScript gives us a special tool for this: the typeof operator!
The typeof operator tells you the type of any value. Here's how to use it:
↗️
Loading editor...
↶
↷
Notice that typeof always returns a string that describes the type. For example, typeof 42 returns the string "number", not the number itself!
Why do we need typeof?
JavaScript is a dynamically typed language, meaning variables can hold different types of values. The typeof operator helps you check what type you're working with before performing operations. This is especially useful when you receive data from users or other sources and need to validate it!
Verify