Want to Contribute to us or want to have 15k+ Audience read your Article ? Or Just want to make a strong Backlink?

How to Check if a Value is an Object in JavaScript



Utilizing typeof operator

JavaScript gives the typeof operator to examine the worth knowledge sort.

The typeof operator returns a string indicating the kind of the operand’s worth.

typeof variable === 'object' returns true for:

  • objects
  • null
  • arrays
  • common expressions
  • new Date()
  • new Map()
  • new Set()

So these validation are required.

code

const isObject = (worth) => {
  return typeof worth === 'object'
  && worth !== null
  && !Array.isArray(worth)
  && !(worth instanceof RegExp)
  && !(worth instanceof Date)
  && !(worth instanceof Set)
  && !(worth instanceof Map)
}
Enter fullscreen mode

Exit fullscreen mode

There are additionally different methods to examine if a price is an object like:

  • Utilizing instanceof operator
  • Utilizing constructor property
  • Utilizing Object.prototype.toString() technique

Add a Comment

Your email address will not be published. Required fields are marked *

Want to Contribute to us or want to have 15k+ Audience read your Article ? Or Just want to make a strong Backlink?