
JavaScript's typeof Operator: Uses and Limitations

In JavaScript we use typeof to determine the type of a variable or value. Whilst it's very straightforward and commonly used, typeof does sometimes yield results that are not as intuitive as one might expect...
What is typeof?
typeof is a unary operator which returns a string indicating the type of the operand you've passed to it. It is often used to check the data type of a variable or value, especially when working with dynamic or unknown data.
typeof syntax
typeof operandoperand: The variable or value whose type we want to determine.
Using typeof
The typeof operator is straightforward to use, so here are a few examples:
console.log(typeof 'Hello'); //=> 'string'console.log(typeof 42); //=> 'number'console.log(typeof true); //=> 'boolean'console.log(typeof undefined); //=> 'undefined'console.log(typeof null); //=> 'object' (This is a known JavaScript quirk)console.log(typeof {}); //=> 'object'console.log(typeof function () {}); //=> 'function'Inaccuracies and Limitations of typeof
You might have noticed in the example above that typeof null returns object. Whilst typeof is useful, it does have several limitations which can lead to misleading results.
Arrays and null are Objects
In JavaScript, arrays and null are considered objects, which can feel particularly counterintuitive. This behaviour stems from JavaScript's early design, where typeof null returns 'object' due to a bug in the language's first implementation that was never corrected.
console.log(typeof [1, 2, 3]); //=> 'object'console.log(typeof null); //=> 'object'Functions are a Special Case
Functions, though objects in nature, are returned as 'function' by typeof. This is a special concession within JavaScript, as functions are first‑class objects with callable abilities.
No Distinction for Newer Types
This is where typeof starts to show it's age. With the evolution of JavaScript (i.e., ES6 and beyond), newer data structures have been introduced like Map, Set, or Symbol. typeof does not distinguish these newer types and often returns 'object' or 'symbol'.
Limited Use for User‑Defined Objects
For custom objects and classes, typeof will return 'object', making it unsuitable for more specific type checks.
Why Understanding typeof is Important
Despite some limitations, understanding and correctly using typeof is an important part of JavaScript development:
- It aids in debugging and validation of function arguments.
- It is useful in conditional statements where type‑checking is required.
- It helps in understanding and dealing with JavaScript's dynamic typing system.
Wrapping up
JavaScript's typeof operator is a quick and easy way to check basic data types, but you should be aware of its limitations and quirks. For more complex type‑checking scenarios, other methods like Array.isArray(), instanceof, or even custom type‑checking functions may be more appropriate.
Related Articles

Will AI Replace Front‑End Developers? 
Understanding Object Types with JavaScript's instanceof. Understanding Object Types with JavaScript's
instanceof
Null and undefined in JavaScript. nullandundefinedin JavaScript
Integrating CMSes with HTML, CSS, and JavaScript. Integrating CMSes with HTML, CSS, and JavaScript

Using CommonJS to Implement Modules in JavaScript. Using CommonJS to Implement Modules in JavaScript
ReferenceError: Window is Not Defined in Gatsby. ReferenceError: Window is Not Defined in Gatsby

Sorting Complex Arrays in JavaScript. Sorting Complex Arrays in JavaScript

Exploring CSS Viewport Units Beyond vw and vh. Exploring CSS Viewport Units Beyond
vwandvh
Parent Selectors in CSS and Sass. Parent Selectors in CSS and Sass

Graph Traversal: Solving the 'Course Schedule' Problem. Graph Traversal: Solving the 'Course Schedule' Problem
Do Websites Need to Look the Same in Every Browser? Do Websites Need to Look the Same in Every Browser?

Resolving mini‑css‑extract‑plugin Warnings in Gatsby. Resolving
mini‑css‑extract‑pluginWarnings in Gatsby