Using the typeof operator
The typeof operator returns one of the following string values:
– “undefined” if the value is undefined
– “boolean” if the value is a boolean
– “number” if the value is a number
– “string” if the value is a string
– “symbol” if the value is a symbol (added in ECMAScript 6)
– “object” if the value is an object or null
– “function” if the value is a function
Using the instanceof operator
The instanceof operator allows you to check whether an object belongs to a specific class or constructor. It checks the prototype chain of an object, making it useful for checking the type of objects created from custom classes. Here’s an example:
value instanceof Array;
The instanceof operator returns true if the object is an instance of the specified class or constructor, and false otherwise.
Using the constructor property
value.constructor === Array;
This code checks if the constructor of the object referenced by “value” is the Array constructor. You can replace “Array” with any other constructor to check for different types.
Using the Object.prototype.toString() method
This code returns a string in the format “[object Type]”, where “Type” represents the type of the value. For example, “[object Array]” for an array, “[object Object]” for an object, and so on.