How to Check if a Value Is Object in JavaScript
-
Use the
instanceof
Function to Check Whether a Value Is an Object or Not in JavaScript -
Use the
typeof()
Function to Check Whether a Value Is an Object or Not in JavaScript - Use User-Defined Functions to Check Whether a Value Is an Object or Not in JavaScript
-
Use the
getPrototypeOf()
Method to Check Whether a Value Is an Object or Not in JavaScript
In JavaScript, every value defined is either an object or a primitive. Any value that is not an object and does not have any methods is known as a primitive. Whatever is not primitive is an object or not.
In this tutorial, we will check whether a value is an object or not.
Use the instanceof
Function to Check Whether a Value Is an Object or Not in JavaScript
We will check the value with the help of the instanceof
method, which returns the type of object at run-time.
In the following example, the function will return true
if the value is of an object type.
const test = {};
function isObject(val) {
return val instanceof Object;
}
console.log(isObject(test));
Output:
true
But this method will not work for all the cases. The isObject(Object.prototype)
and isObject(Object.create(null))
will return False.
Use the typeof()
Function to Check Whether a Value Is an Object or Not in JavaScript
We can check the type of objects using the typeof()
function.
For example,
const test = {};
function isObject(val) {
return (typeof val === 'object');
}
console.log(isObject(test));
Output:
true
This method will also not work for all the test cases. It will return false positives for the null test cases and false negatives for functions. The isObject(Object)
will also return False.
We can also define another function using this function that will address the above-mentioned limitations.
See the code below.
const test = {};
function t() {};
function isObject(val) {
if (val === null) {
return false;
}
return ((typeof val === 'function') || (typeof val === 'object'));
}
console.log(isObject(test));
console.log(isObject(t));
Output:
true
true
The function in the above example returns False if the value is NULL. If the value is of type function
or type object
, it returns true. Otherwise, it returns False.
Use User-Defined Functions to Check Whether a Value Is an Object or Not in JavaScript
We can define more such functions to get the job done based on the object’s constructor and class.
For example,
const test = {};
function isObject(obj) {
return obj === Object(obj);
}
function isObject2(obj) {
return obj.constructor.toString().indexOf('Object') > -1;
}
console.log(isObject(test));
console.log(isObject2(test));
Output:
true
true
In the above example, we created two functions that could check whether the given value is an object.
Use the getPrototypeOf()
Method to Check Whether a Value Is an Object or Not in JavaScript
The getPrototypeOf
method will throw an exception if its argument is not an object. We can use it to check the type of class of the given value.
For example,
const test = {};
const object1 = Object.create(test);
console.log(Object.getPrototypeOf(object1) === test);
Output:
true