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)
}
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