Skip to main content

Command Palette

Search for a command to run...

Understanding JavaScript's `typeof` Operator

Published
โ€ข2 min readโ€ขView as Markdown
Understanding JavaScript's `typeof` Operator
D

Hi ๐Ÿ‘‹, I am Dipankar Paul, aspiring Full Stack Developer with a passion for learning new technologies. Currently, I am learning my front-end development and full-stack development through Apna College Delta. With a passion for creating innovative and user-friendly applications, I am excited to continue my journey in the tech industry.

Introduction

The typeof operator in JavaScript is like your digital detective, helping you identify the data type of a variable or value. It provides a neat way to peek into the nature of your data. Whether you're working with conditional statements or need to debug your code, typeof is your go-to tool.

Let's start with the syntax:

typeof operand

The operand here is the value or variable you're curious about. It's as simple as that!

Examples

Now, let's dive into some practical examples:

Numeric Insights

let age = 25;
console.log(typeof age); // Output: "number"

String Speculation

let name = "John";
console.log(typeof name); // Output: "string"

The Boolean Breakdown

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

Object Orientation

let person = { firstName: "John", lastName: "Doe" };
console.log(typeof person); // Output: "object"

Arrays Unveiled

let fruits = ["apple", "banana", "orange"];
console.log(typeof fruits); // Output: "object"

The Function Frontier

let calculateArea = function(width, height) {
return width * height;
};
console.log(typeof calculateArea); // Output: "function"

Caution

However, there are a few things to keep in mind when dealing with the typeof operator:

  • Null's Identity Crisis: If your operand is null, JavaScript may play a trick on you by returning "object." It's a known quirk in JavaScript.

  • Function Foreshadowing: Functions have a special place in JavaScript's heart. So, if your operand is a function, typeof will spill the beans and reveal "function."

  • Array Ambiguity: Arrays, being a type of object in JavaScript, will also perplex you by showing "object" when you use typeof.

Key Takeaways

  • The typeof operator is used to determine the data type of a value or variable in JavaScript.
  • It returns a string indicating the type of the operand.
  • The typeof operator can help with conditional statements or debugging to check the data type of a variable.
  • The typeof operator has some caveats, such as returning "object" for null and arrays, and returning "function" for functions.

Javascript

Part 19 of 24

In this series I will be sharing my journey in learning Javascript and posting my own notes from the process. These notes can be helpful for beginners in learning this amazing language. Good luck.

Up next

Mastering the 'in' Operator in JavaScript

Introduction In the world of JavaScript, there's a hidden detective known as the in operator. Its role? To determine whether a specific property exists within an object or if an elusive element is concealed within an array. This operator returns a si...

More from this blog

Dipankar Paul's blog

51 posts