DEV Community

Cover image for How to check if a variable is of type object in JavaScript?
MELVIN GEORGE
MELVIN GEORGE

Posted on • Originally published at melvingeorge.me

How to check if a variable is of type object in JavaScript?

Originally posted here!

To check if a variable is an object, you can use the typeof operator followed by the variable and we also need to check if the variable is not null value because null is of type object in JavaScript.

// Check if variable is an object
const variable = {
  name: "John Doe",
  age: 23,
};

typeof variable === "object" && variable !== null; // true

/*
 Boolean true means the variable is of object type
*/
Enter fullscreen mode Exit fullscreen mode
  • If the above condition returns true, then the variable is of type object.
  • This is a better way to check if a variable is of type object.

See the above code live in JSBin.

Feel free to share if you found this useful 😃.


Top comments (1)

Collapse
 
thedamon profile image
Damon Muma

Should be noted that Arrays are Objects in js, so this may not be what people are looking for