Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is typeof in JavaScript

Understanding the "typeof" Operator

Imagine you're in a dark room, filled with all sorts of objects. You can't see anything, but you can touch and feel. You pick up an object, you feel it's round, small, and smooth. You instantly know it's a ball. This is what "typeof" does in JavaScript. It's like your sense of touch in the dark room of code. It tells you what type of data you're dealing with.

The Basics Of "typeof"

In JavaScript, we use the "typeof" operator to find out the type of a given value or variable. Here's a basic example.

let message = "Hello, World!";
console.log(typeof message); // "string"

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

let isTrue = true;
console.log(typeof isTrue); // "boolean"

As you can see, the "typeof" operator returns a string indicating the type of the unevaluated operand. An operand is the term used for any object that is capable of being manipulated in some way. In this case, our operands are the variables message, number, and isTrue.

The Different Data Types in JavaScript

JavaScript has six main data types that can be identified using the "typeof" operator: undefined, boolean, number, string, object, and function.

Let's dive a little deeper into each one.

Undefined

let test;
console.log(typeof test); // "undefined"

An undefined type is a variable that has been declared but has not yet been assigned a value.

Boolean

let isReading = true;
console.log(typeof isReading); // "boolean"

A boolean type has one of two possible values: true or false.

Number

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

A number type is an integer or floating point number.

String

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

A string type is a sequence of characters.

Object

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

An object is a collection of named values.

Function

function greet() {
  return "Hello, World!";
}
console.log(typeof greet); // "function"

A function is a block of code designed to perform a particular task.

Special Cases Of "typeof"

There are few special cases you should be aware of when using "typeof".

console.log(typeof null); // "object"
console.log(typeof Array); // "function"
console.log(typeof Date); // "function"
console.log(typeof NaN); // "number"

The "typeof" operator returns "object" for "null" because, due to a bug in JavaScript, null is considered a type of object. For "Array" and "Date", it returns "function", which is also considered a type of object. Finally, for "NaN" (which stands for Not a Number), it ironically returns "number".

Conclusion

The "typeof" operator in JavaScript is like a trusty flashlight in a dark room of code, illuminating the data type of any given value or variable. Although it's simple and straightforward to use, it does have a few peculiar quirks, like considering null and NaN as an object and number respectively. But once you get the hang of it, it's a valuable tool in your JavaScript toolkit.

Just like in the dark room, you might initially get confused and misjudge an object's type, but as you familiarize yourself more and more, you start getting it right. The same is true for JavaScript's "typeof". As you continue your journey learning JavaScript and coding, you'll get better at identifying data types and dealing with them.

So, keep coding, keep exploring, and remember, every master once was a beginner. Happy coding!