You are currently viewing You Need to Know: How to Detect Object in JavaScript?

You Need to Know: How to Detect Object in JavaScript?

In this blog post, we’ll explore the various ways to detect object in JavaScript, and other types also. We will go through in the depth of it and will make sure after reading this, you don’t have to search online on how to check if a value is an object etc, because internet is filled with this search.

Hello Weekendtutorial family,

It is one of the most asked question in frontend interviews. This checks your knowledge on objects in javascript.

In JS, you have 2 ways to detect object in javascript –

  1. using typeof operator
  2. using Object.prototype.toString method

Method to detect Object in javascript

1. Using typeof

This is one of the most common technique to check the type of a given value. Since everything in javascript is an object, so we have to carefully use this method as it also gives “object” for following –

				
					console.log(typeof {}); // "object"
console.log(typeof []); // "object"
console.log(typeof null); // "object"
				
			

So, here we have to remove the “[]” and “null” check to say if value is an indeed object.

				
					function isObject(value) {
  return typeof value === "object" && value !== null && !(value instanceof Array);
}

// Now calling above gives -
console.log(isObject({})); // true
console.log(isObject([])); // false
console.log(isObject(null)); // false
				
			

This way we can use the object detection but beware that typeof new String will also give “object”. So, this is not the complete solution and may require refinement based on the use cases.

Lets check the other solution which is handling almost all cases.

2. Using Object.prototype.toString.call

We have must use this for Array detection but this is not limited to just an array but it can detect objects and other types as well.

Let me put some console.log for various inputs tried –

				
					const typeString = Object.prototype.toString;

console.log(typeString.call({})); // "[object Object]"
console.log(typeString.call([])); // "[object Array]"
console.log(typeString.call(null)); // "[object Null]"
console.log(typeString.call(function () {})); // "[object Function]"
console.log(typeString.call(undefined)); // "[object Undefined]"
console.log(typeString.call('')); // "[object String]"
console.log(typeString.call(new String)); // "[object String]"
				
			

So, here I have stored the Object.prototype.toString method in a variable so that i don’t have to call it again and again.

Now, from the output, you can see we can accurately say if an input is an object or not. Let me write the isObject function again using this method.

				
					function isObject(value) {
  const typeString = Object.prototype.toString;
  
  return typeString.call(value).indexOf('Object') > -1;
}

// Now calling above gives -
console.log(isObject({})); // true
console.log(isObject([])); // false
console.log(isObject(null)); // false
				
			

This is the most reliable and accurate way to detect the types in javascript. Of course, the above method can be extended further to detect the other types also.

Summary

We have 2 methods of detecting the objects and other types in javascript.

  1. Learning the nuances of the typeof operator is must
  2. Object.prototype.toString.call() is the most reliable method to detect objects.

Understanding and knowing the types of variables and how to detect them will make you an awesome developer. This is a crucial skill for writing efficient and error-free code.

If you like the article, please share it among your friends.

That’s it for this article, I hope you have understood the content in detail.

If you like to read another article on javascript, you may read the following articles

  1. Generate all subarrays in javascript
  2. Debouncing and throttling in javascript
  3. How to refactor switch statements in javascript?

Click to Checkout Complete javascript articles

Please share the articles on –

Facebook
Twitter
LinkedIn