How to check the type of object or variable in JavaScript?

Checking the type of object or variable is significant in any programming language, especially in JavaScript.

In this article, we will walk you through how to do it using several methods to accomplish this task effectively.

So, let’s get started and explore several ways to check the type of an object or variable in JavaScript.

What is data type in JavaScript?

There are seven primitive types that are not objects in in JavaScript. It includes the following:

📌BigInt

📌Boolean

📌null

📌Number

📌String

📌Symbol

📌Undefined

These primitive types are the basic building blocks in JavaScript. On the other hand, anything that is not one of these primitive types is considered an object.

This includes arrays and functions, which are treated as objects because they are collections of data or executable code.

An object in JavaScript is like a container that can hold multiple values or functions, organized as key-value pairs.

Objects allow us to represent more complex data structures and perform various operations on them.

Solutions on how to check the type of object in JavaScript

The following are the solutions which you can use to check the type of object in JavaScript.

Solution 1: Use the typeof operator

The typeof operator gives you a string that tells you the type of the value stored in the operand.

To check the type of a variable or object, you can use the “typeof” operator. It takes a variable and gives you its type as a string.

Syntax

typeof operand

or

typeof variable_name 

For example:

console.log(typeof 18);
// output: "number"

console.log(typeof "Itsourcecode");
// output: "string"

console.log(typeof false);
// output: "boolean"

console.log(typeof undeclaredVariable);
// output: "undefined"

Output:

number
string
boolean
undefined

Here’s another example code:

let sample = "Itsourcecode";
console.log(typeof sample); // Output: string

Output:

string

Examples of typeof operator in JavaScript

typeof 18 === "number";
typeof 1.5 === "number";
typeof Math.LN2 === "number";
typeof Infinity === "number";
typeof NaN === "number"; // despite if it is Not a Number
typeof Number("1") === "number"; // The Number tries to convert different types of data into numbers.
typeof Number("Itsourcecode") === "number"; // This includes values that cannot be directly converted into a number.

typeof 18n === "bigint";

typeof true === "boolean";
typeof false === "boolean";
typeof Boolean(0) === "boolean"; 
typeof !!1 === "boolean"; //Using the logical NOT (!) operator twice is equivalent to using the Boolean() function.

typeof "" === "string";
typeof "Itsourcecode" === "string";
typeof `Itsourcecode` === "string";
typeof "3" === "string"; // always remember that any number within a string is still typeof string

typeof Symbol() === "symbol";
typeof Symbol("itsourcecode") === "symbol";
typeof Symbol.iterator === "symbol";

let x;
typeof x;  //'undefined'
typeof y;  //'undefined'
typeof undefined;  //'undefined'

typeof null;  //'object';

typeof { x: 1 } === "object";
typeof [1, 2, 4, 4, 5] === "object";

typeof new Date() === "object";
typeof /regex/ === "object";

typeof new Boolean(true) === "object";
typeof new Number(1) === "object";
typeof new String("xyz") === "object";

typeof function () {} === "function";
typeof class C {} === "function";
typeof Math.sin === "function";
typeof (() => {});  //'function';
typeof Math.sqrt;  //'function'

Solution 2: Use instanceof operator

In JavaScript, you can use the “instanceof” operator to check if an object is created from a specific constructor. It will give you a boolean (true or false) result.

Syntax:

object_name instanceof object_constructor 

Here’s an example:

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

Output:

true

Solution 3: Use Object.prototype.toString.call()

Another way to check the type of an object in JavaScript is by using the Object.prototype.toString() method to find out the class of an object.

It provides a clear and definitive way to determine the class of an object.

Here’s an example:

let sampleDate = new Date();
console.log(Object.prototype.toString.call(sampleDate)); // Output: [object Date]

Output:

[object Date]

Solution 4: Use constructor property

The constructor property is a property of an object that points to the constructor function responsible for creating the object. In simple terms, it reveals the type of the object.

For instance, when you create an array using the Array constructor, the constructor property of that array will refer to the Array constructor function.

You can use the constructor property of an object to determine its type.

let sampleArray = [1, 2, 3, 4, 5];
console.log(sampleArray.constructor === Array); // Output: true

Output:

true

Conclusion

In conclusion, this article discussed several methods to effectively check the type of object or variable in JavaScript.

By understanding the data types in JavaScript and recognizing the distinction between primitive types and objects, developers can effectively perform type checks and make informed decisions in their code.

The article explored four different solutions for type checking.

The typeof operator can be used to obtain the type of a variable or object by returning a string representation of its type.

The instanceof operator allows checking if an object is created from a specific constructor, providing a boolean result.

Another approach is using the Object.prototype.toString.call() method, which gives a string representation of an object’s type.

Additionally, the constructor property can be utilized to determine the type of an object by comparing it with the corresponding constructor function.

By employing these methods, developers can accurately identify the types of objects and variables in JavaScript, enabling them to write more robust and error-free code.

We are hoping that this article provides you with enough information that helps you understand the JavaScript check type of object.

You can also check out the following article:

Thank you for reading itsourcecoders 😊.

Leave a Comment