How to Check Type in JavaScript

JavaScript is a dynamically typed language, which means that you don’t have to declare the type of a variable when you create it. However, sometimes you need to check the type of a variable to ensure that your code behaves as expected. In this article, we’ll explore different ways to check the type of a variable in JavaScript.

1. Using the typeof Operator

The typeof operator is one of the most common ways to check the type of a variable in JavaScript. It returns a string indicating the type of the variable.

Example 1: Basic Usage

let num = 5;
let str = "Hello";
let bool = true;
let und = undefined;

console.log(typeof num);   // Output: "number"
console.log(typeof str);   // Output: "string"
console.log(typeof bool);  // Output: "boolean"
console.log(typeof und);   // Output: "undefined"

Example 2: Checking null

let obj = null;

console.log(typeof obj);   // Output: "object"

Note: The typeof operator returns "object" for null, which can be misleading. To check if a variable is null, you should use strict equality (===).

Example 3: Checking Arrays

let arr = [1, 2, 3];

console.log(typeof arr);   // Output: "object"

Note: The typeof operator returns "object" for arrays, which can be confusing. To check if a variable is an array, you can use the Array.isArray() method.

2. Using the instanceof Operator

The instanceof operator checks if a variable is an instance of a specific type. It is often used to check if a variable is an array, a date, or a regular expression.

Example 1: Checking Arrays

let arr = [1, 2, 3];

console.log(arr instanceof Array);   // Output: true

Example 2: Checking Dates

let date = new Date();

console.log(date instanceof Date);   // Output: true

Example 3: Checking Regular Expressions

let regex = /hello/;

console.log(regex instanceof RegExp);   // Output: true

3. Using Object.prototype.toString

The Object.prototype.toString method can be used to get a string representation of the type of a variable. This method is more reliable than the typeof operator for certain cases, such as checking if a variable is an array.

Example 1: Checking Arrays

let arr = [1, 2, 3];

console.log(Object.prototype.toString.call(arr));   // Output: "[object Array]"

Example 2: Checking Dates

let date = new Date();

console.log(Object.prototype.toString.call(date));   // Output: "[object Date]"

Example 3: Checking null and undefined

let obj = null;
let und = undefined;

console.log(Object.prototype.toString.call(obj));   // Output: "[object Null]"
console.log(Object.prototype.toString.call(und));   // Output: "[object Undefined]"

4. Using the Symbol Type

In ES6, a new primitive type called Symbol was introduced. You can check if a variable is a symbol using the typeof operator.

Example 1: Checking Symbols

let sym = Symbol('symbol');

console.log(typeof sym);   // Output: "symbol"

5. Using the BigInt Type

In ES6, a new primitive type called BigInt was introduced. You can check if a variable is a BigInt using the typeof operator.

Example 1: Checking BigInt

let bigInt = 123456789012345678901234567890n;

console.log(typeof bigInt);   // Output: "bigint"

6. Best Practices

  • Use typeof for checking primitive types.
  • Use instanceof for checking objects, arrays, dates, and regular expressions.
  • Use Object.prototype.toString for more reliable type checking, especially for arrays and null.
  • Use Array.isArray() to check if a variable is an array.
  • Use typeof for checking symbols and BigInts.

7. Frequently Asked Questions

Q1: Why does typeof null return "object"?

This is a known bug in JavaScript. The typeof operator returns "object" for null because null was originally intended to be an object in JavaScript.

Q2: How can I check if a variable is null?

You can use strict equality (===) to check if a variable is null.

let obj = null;

console.log(obj === null);   // Output: true

Q3: How can I check if a variable is an array?

You can use the Array.isArray() method to check if a variable is an array.

let arr = [1, 2, 3];

console.log(Array.isArray(arr));   // Output: true

Q4: How can I check if a variable is a symbol?

You can use the typeof operator to check if a variable is a symbol.

let sym = Symbol('symbol');

console.log(typeof sym);   // Output: "symbol"

Q5: How can I check if a variable is a BigInt?

You can use the typeof operator to check if a variable is a BigInt.

let bigInt = 123456789012345678901234567890n;

console.log(typeof bigInt);   // Output: "bigint"

Conclusion

Checking the type of a variable in JavaScript is an essential skill for any developer. By using the typeof operator, instanceof, Object.prototype.toString, and other methods, you can ensure that your code behaves as expected. Remember to use the right method for the right situation and always test your code thoroughly.

Index
Scroll to Top