Usage of typeof

In JavaScript, the typeof operator is a fundamental tool for developers to understand the type of data they're working with. It's essentially a built-in function that helps determine the type of a variable or an expression. Imagine it as a detective tool that tells you what kind of data you're dealing with.

Basic Usage of "typeof"

The basic usage of typeof is quite straightforward. You use it by placing it before the operand (variable, object, or expression) you want to examine. It returns a string that indicates the type of the operand. This information can be crucial for making decisions in your code.

Example:

				
					let x = 10;
console.log(typeof x); // Output: "number"

				
			

In this example, we declare a variable x and assign it the value 10. Then, we use the typeof operator to determine the type of x. Since x holds a numeric value, typeof x returns "number". This demonstrates the basic usage of typeof for checking the type of a variable.

Types Returned by "typeof"

Understanding the types returned by typeof is essential. Let’s break them down:

  1. “undefined”: This type is returned if the operand is undefined. Undefined means that the variable has been declared but not yet assigned a value.
  2. “boolean”: If the operand is a boolean value, meaning it can either be true or false, typeof will return this type.
  3. “number”: When the operand is a numeric value, typeof will return this type.
  4. “string”: If the operand is a sequence of characters enclosed in quotes (either single or double), it’s considered a string, and typeof will return this type.
  5. “bigint”: Introduced in ECMAScript 2020, BigInt represents whole numbers larger than 2^53 – 1. If the operand is a BigInt, typeof will return this type.
  6. “symbol”: Symbols are unique and immutable data types introduced in ECMAScript 2015. If the operand is a symbol, typeof will return this type.
  7. “object”: When the operand is an object (except for null), typeof will return this type. Objects are collections of key-value pairs or properties.
  8. “function”: If the operand is a function, typeof will return this type. Functions are blocks of reusable code that perform a specific task.

Example:

				
					let a;
console.log(typeof a); // Output: "undefined"

let b = true;
console.log(typeof b); // Output: "boolean"

let c = 42;
console.log(typeof c); // Output: "number"

let d = "Hello";
console.log(typeof d); // Output: "string"

let e = BigInt(123);
console.log(typeof e); // Output: "bigint"

let f = Symbol("foo");
console.log(typeof f); // Output: "symbol"

let g = {};
console.log(typeof g); // Output: "object"

let h = function() {};
console.log(typeof h); // Output: "function"

				
			

Handling Edge Cases

One notable edge case is the behavior of typeof null. Despite null technically being an object in JavaScript, typeof null returns "object". This behavior is considered a quirk of the language and has been maintained for compatibility reasons. It’s essential to be aware of this when using typeof in your code.

Example:

				
					let i = null;
console.log(typeof i); // Output: "object"

				
			

In this example, we assign the value null to the variable i. Despite null being technically an object in JavaScript, typeof null returns "object". This behavior is a well-known quirk of the language and has been maintained for compatibility reasons. It’s important to be aware of this when using typeof for type checking.

In this chapter, we've delved into the world of typeof in JavaScript. By mastering this operator, you gain a powerful tool for understanding the types of data your code is working with. From basic usage to handling edge cases, typeof provides valuable insights that can enhance the robustness and reliability of your JavaScript programs.This combination of theory and code examples provides a comprehensive understanding of the typeof operator in JavaScript, ensuring that developers have the knowledge and tools necessary for effective type checking and handling in their code. Happy coding !❤️

Table of Contents

Contact here

Copyright © 2025 Diginode

Made with ❤️ in India