How do you check if a variable is an object in JavaScript?

In JavaScript, an object is a collection of properties, where each property has a name (also known as a key) and a value.

A variable in JavaScript can be an object or another data type, such as a string, number, or boolean.

There are several ways by which we can determine the type of a variable is object.

1. Using typeof operator:

To check if a variable is an object in JavaScript, you can use the typeof operator. The typeof operator returns a string that represents the data type of the operand.

For example:

let myVariable = { prop1: "value1", prop2: "value2" };
console.log(typeof myVariable); // outputs "object"

In this example, the typeof operator returns "object", indicating that myVariable is an object.

2. Using toString method:

One way to check if a variable is an object is to use the Object.prototype.toString method. This method returns a string that represents the object’s constructor function.

For example:

let myVariable = { prop1: "value1", prop2: "value2" };
console.log(Object.prototype.toString.call(myVariable)); // outputs "[object Object]"

In this example, Object.prototype.toString.call(myVariable) returns "[object Object]", indicating that myVariable is an object.

However, this approach has some limitations as well. For example, it doesn’t work for variables that are created using custom constructor functions

3. Using instanceof operator:

Another way to check if a variable is an object is to use the instanceof operator. This operator checks if an object is an instance of a particular class.

For example:

let myVariable = { prop1: "value1", prop2: "value2" };
console.log(myVariable instanceof Object); // outputs "true"

In this example, myVariable instanceof Object returns true, indicating that myVariable is an object.

For example, it doesn’t work for variables that are created using custom constructor functions, and it returns false for null.

Use combinations of technique to check for object:

first combination:

function isObject(variable) {
  return (
    Object.prototype.toString.call(variable) === "[object Object]" &&
    variable !== null
  );
}
let myVariable = { prop1: "value1", prop2: "value2" };
console.log(isObject(myVariable)); // outputs "true"

The isObject method first checks if variable is object and then it checks if it is not null.

second combination:

function isPlainObject(variable) {
  return (
    typeof variable === "object" &&
    variable !== null &&
    !Array.isArray(variable)
  );
}

let myVariable = { prop1: "value1", prop2: "value2" };
console.log(isPlainObject(myVariable)); // outputs "true"

In the above code, the isPlainObject function checks if the variable is an object by using typeof and checking that variable is not null and not an array. This function returns true if the variable is a plain object and false otherwise.

Thank you for reading, and let’s have conversation with each other

Thank you for reading my article. Let’s have conversation on Twitter and LinkedIn by connecting.

Read more: