Member-only story
Advanced Guide to JavaScript typeof
Operator
The typeof
operator in JavaScript is used to determine the type of a given value or variable at runtime. While it seems simple at first glance, understanding its quirks and advanced use cases is essential for mastering type checking and handling dynamic behavior in JavaScript.
This guide will dive deep into the advanced aspects of typeof
, highlighting how it behaves, the edge cases, and how you can use it effectively in modern JavaScript applications.
Basic Syntax and Usage
The syntax of typeof
is straightforward:
typeof operand
Where operand
is the value whose type you want to check. typeof
returns a string that represents the type of the operand.
Examples:
console.log(typeof 42); // "number"
console.log(typeof 'Hello World'); // "string"
console.log(typeof true); // "boolean"
console.log(typeof undefined); // "undefined"
console.log(typeof {}); // "object"
console.log(typeof function(){}); // "function"
console.log(typeof Symbol()); // "symbol"
The basic types returned by typeof
are:
- “undefined” — for
undefined
values. - “object” — for objects, arrays,
null
…