How to check if a value is object-like in JavaScript ?
Last Updated :
29 May, 2024
In JavaScript, objects are a collection of related data. It is also a container for name-value pairs. In JavaScript, we can check the type of value in many ways. Basically, we check if a value is object-like using typeof, instanceof, constructor, and Object.prototype.toString.call(k). All of the operators are used in some specific conditions.
There are several methods that can be used to check if a value is object-like in JavaScript.
Approach 1: Using typeof operator
This is used to identify the type of variable. It returns a variable type. It is the easiest way to check the type of the variable. It works for some variables, but it did not identify the exact type of variable.
Conditions: It treated array, set, and null the same as the object. typeof returns object for all of these. In the case of variables except three of this typeof is used.
Syntax:
typeof VariableName;
Example: In this example, we are using typeof operator.
JavaScript
let k = { Name: 'gfg', Country: 'India' };
let k0 = new Set()
let k1 = [1, 2, 3];
let k2 = "hello";
let k3 = null;
let k4 = undefined;
console.log(typeof k);
console.log(typeof k0);
console.log(typeof k1);
console.log(typeof k2);
console.log(typeof k3);
console.log(typeof k4);
Outputobject
object
object
string
object
undefined
Approach 2: Using instanceof operator
This is used to check if any instance is made with a certain constructor or not. It returns true if it is made with constructor else returns false. It only works for those who are wrapped in regular object types.
Condition: It treats the array and sets the same as the object. So we can use instanceof operator for all values except these two.
Syntax:
Variable instanceof object;
Example: In this example, we are using instanceof operator.
JavaScript
let k = { Name: 'gfg', Country: 'India' };
let k0 = new Set()
let k1 = [1, 2, 3];
let k2 = "hello";
let k3 = null;
let k4 = undefined;
console.log(k instanceof Object);
console.log(k0 instanceof Object);
console.log(k1 instanceof Object);
console.log(k2 instanceof Object);
console.log(k3 instanceof Object);
console.log(k4 instanceof Object);
Outputtrue
true
true
false
false
false
Approach 3: Using constructor property
This is the property of the variable which points to the fundamental object constructor type of that object. We can check for those variables which have constructor property.
Condition: The constructor method throw an error for variables not having constructor property. null and undefined don’t have a constructor property, so it throws an error.
Syntax:
Variable.constructor === Object
Example: In this example, we are using constructor property.
JavaScript
let k = { Name: 'gfg', Country: 'India' };
let k0 = new Set()
let k1 = [1, 2, 3];
let k2 = "hello";
console.log(k.constructor === Object)
console.log(k0.constructor === Object)
console.log(k1.constructor === Object)
console.log(k2.constructor === Object)
Outputtrue
false
false
false
In this approach, we are using the Object.prototype.toString() method to get the internal [[Class]] property of the value and compare it with “[object Object]”, determining if the value is object-like in JavaScript.
Syntax:
function isObjectLike(value) {
return Object.prototype.toString.call(value) === "[object Object]";
}
Example: In this Using Object.prototype.toString() method, the example checks if the value is object-like and returns true for {} and false for [], null, and “string”.
JavaScript
function isObjectLike(value) {
return Object.prototype.toString.call(value) === "[object Object]";
}
console.log(isObjectLike({})); // true
console.log(isObjectLike([])); // false
console.log(isObjectLike(null)); // false
console.log(isObjectLike("string")); // false
Outputtrue
false
false
false
Using Lodash’s _.isObjectLike function
Lodash’s _.isObjectLike function checks if a value is object-like. It returns true for values that are not `null` and have a type of `”object”`, including arrays and plain objects. It’s useful for quickly determining if a value behaves like an object.
Example:
JavaScript
const _ = require('lodash');
function isObjectLike(value) {
return _.isObjectLike(value);
}
console.log(isObjectLike({})); // true
console.log(isObjectLike([])); // true (arrays are objects)
console.log(isObjectLike(null)); // false
Output:
true
true
false
Similar Reads
How to get dynamic access to an object property in JavaScript ?
In JavaScript, an object is a collection of properties, where each property consists of a key-value pair. Objects are a fundamental data type in JavaScript. You can create an object in JavaScript in the following ways: By using the object literal notation, which is a comma-separated list of key-valu
7 min read
How to Remove an Entry by Key in JavaScript Object?
In JavaScript, objects store data in the form of key-value pairs where the key may be any property of the object. In this article let us see how to remove key-value pairs a by given key in the object. Table of Content Using the delete operatorUsing the filter() methodUsing Destructuring and Object.a
3 min read
How to modify an object's property in an array of objects in JavaScript ?
Modifying an object's property in an array of objects in JavaScript involves accessing the specific object within the array and updating its property. Using the Array.map() methodUsing the map() method to create a new array by transforming each element of the original array based on a specified func
5 min read
How to compare Arrays of Objects in JavaScript?
In JavaScript, comparing arrays of objects can be more complex than comparing primitive data types. We will discuss different ways to compare arrays of objects effectively, with detailed code examples and explanations. Syntax: Before going to detail the comparison techniques, let's first understand
5 min read
How to create object properties in JavaScript ?
JavaScript is built on an object-oriented framework. An object is a collection of properties, where each property links a key to a value. These properties are not in any specific order. The value of a JavaScript property can be a method (function). Object properties can be updated, modified, added,
4 min read
How to create an object with prototype in JavaScript ?
In this article, we will discuss object creation & prototypes, along with understanding the different ways for object creation & their implementation through the examples. Prototypes are the mechanism by which objects in JavaScript inherit features from another object. A prototype property i
4 min read
How to declare object with computed property name in JavaScript ?
In this article, we learn how to declare an object with a computed property name. Before beginning this article, we have to know about the javascript objects. Computed Property Names: The ES6 computed property names feature allows us to compute an expression as a property name on an object. Javascri
2 min read
How to add and remove properties from objects in JavaScript ?
We will try to understand how to add properties to an object and how to add or remove properties from an object in JavaScript. Before we go and see the addition and removal of properties from an object let us first understand the basics of an object in JavaScript. Object: An object in JavaScript is
6 min read
How to check if a value is object-like in JavaScript ?
In JavaScript, objects are a collection of related data. It is also a container for name-value pairs. In JavaScript, we can check the type of value in many ways. Basically, we check if a value is object-like using typeof, instanceof, constructor, and Object.prototype.toString.call(k). All of the ope
4 min read
JavaScript - Convert Two-Dimensional Array Into an Object
Here are the different methods to convert the two-dimensional array into an object in JavaScript. 1. Using a for LoopThe simplest way to convert a two-dimensional array into an object is by iterating through the array using a for loop and assigning key-value pairs. [GFGTABS] JavaScript const a = [[
2 min read