How Do You Check Data Type in JS?

//

Larry Thompson

When working with JavaScript, it is important to be able to check the data type of a variable. This can help you ensure that your code is executing correctly and that you are working with the expected data. In this tutorial, we will explore different methods to check the data type in JavaScript.

Using the typeof Operator

The typeof operator is a built-in operator in JavaScript that allows you to determine the data type of a given variable. It returns a string indicating the type of the operand.

Here’s an example:


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

In this example, we declared a variable num and assigned it a value of 42. By using typeof num, we were able to determine that its data type is “number”.

Using instanceof Operator

The instanceof operator checks whether an object belongs to a specific class or constructor function. It returns true if the object is an instance of the specified class, and false otherwise.

To illustrate this, consider the following example:


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

In this example, we created an array object arr. We used instanceof Array, which returned true, indicating that arr is an instance of the Array class.

Using Object.prototype.toString()

The Object.toString() method returns a string representing the object’s type. It is more reliable than typeof, especially for checking complex data types like arrays and objects.


let str = "Hello, World!";
let obj = { name: "John", age: 25 };
let arr = [1, 2, 3];

console.log(Object.toString.call(str)); // Output: [object String]
console.call(obj)); // Output: [object Object]
console.call(arr)); // Output: [object Array]

In this example, we used Object.toString() along with call() to check the data types of different variables. The output shows the type enclosed within square brackets.

Note:

  • null, undefined, and primitive values (number, string, boolean) do not have their own constructors. Therefore, using instanceof on them will always return false. To check for these values, it is recommended to use the typeof operator.
  • Date, Error, and other built-in objects can also be checked using the above methods.
  • If you are working with ES6 classes or custom constructors, you can use the instanceof operator to check their instances.

In conclusion, there are several ways to check data types in JavaScript. The typeof operator is useful for basic data types like numbers and strings, while instanceof is more suitable for checking object types.

The Object.toString() method can handle complex data types effectively. Understanding these methods will help you write more robust JavaScript code.

Discord Server - Web Server - Private Server - DNS Server - Object-Oriented Programming - Scripting - Data Types - Data Structures

Privacy Policy