How to check type of variable in javascript?

Software
AffiliatePal is reader-supported. When you buy through links on our site, we may earn an affiliate commission.

Listen

Introduction

In JavaScript, it is essential to be able to check the type of a variable to ensure that it is being used correctly in your code. Whether you need to determine if a variable is a number, a string, an array, or an object, JavaScript provides several methods to check the type of a variable. In this article, we will explore these methods and learn how to effectively check the type of a variable in JavaScript.

Using the typeof Operator

The most common way to check the type of a variable in JavaScript is by using the `typeof` operator. The `typeof` operator returns a string indicating the type of the operand. Here’s an example:

“`javascript
let num = 42;
console.log(typeof num); // Output: “number”

let str = “Hello, World!”;
console.log(typeof str); // Output: “string”

let arr = [1, 2, 3];
console.log(typeof arr); // Output: “object”

let obj = { name: “John”, age: 30 };
console.log(typeof obj); // Output: “object”
“`

As you can see, the `typeof` operator returns “number” for a numeric variable, “string” for a string variable, and “object” for both arrays and objects.

Using the instanceof Operator

Another way to check the type of a variable in JavaScript is by using the `instanceof` operator. The `instanceof` operator tests whether an object has a specific type, returning `true` or `false`. Here’s an example:

“`javascript
let num = 42;
console.log(num instanceof Number); // Output: false

let str = “Hello, World!”;
console.log(str instanceof String); // Output: false

let arr = [1, 2, 3];
console.log(arr instanceof Array); // Output: true

let obj = { name: “John”, age: 30 };
console.log(obj instanceof Object); // Output: true
“`

In this example, the `instanceof` operator returns `false` for numeric and string variables because they are primitive types. However, it returns `true` for array and object variables.

Using the Object.prototype.toString() Method

The `Object.prototype.toString()` method can also be used to check the type of a variable in JavaScript. This method returns a string representing the object’s type. Here’s an example:

“`javascript
let num = 42;
console.log(Object.prototype.toString.call(num)); // Output: “[object Number]”

let str = “Hello, World!”;
console.log(Object.prototype.toString.call(str)); // Output: “[object String]”

let arr = [1, 2, 3];
console.log(Object.prototype.toString.call(arr)); // Output: “[object Array]”

let obj = { name: “John”, age: 30 };
console.log(Object.prototype.toString.call(obj)); // Output: “[object Object]”
“`

As you can see, the `Object.prototype.toString()` method provides a more detailed representation of the object’s type, including the specific type name enclosed in square brackets.

Conclusion

Checking the type of a variable in JavaScript is crucial for ensuring that the code behaves as expected. In this article, we explored three methods for checking the type of a variable: the `typeof` operator, the `instanceof` operator, and the `Object.prototype.toString()` method. Each of these methods has its own advantages and use cases, so it’s important to choose the one that best fits your specific needs.

By understanding how to check the type of a variable in JavaScript, you can write more robust and reliable code that handles different data types appropriately.

References

– developer.mozilla.org
– www.w3schools.com
– www.javascripttutorial.net