Skip to content

JavaScript check if object | Example code

  • by

Try using the typeof() method or instanceof method to check if the object is in JavaScript. You can use it with the if statement.

However, this check will return true for arrays, null, and other objects as well. If you want to specifically check if a value is a plain object (i.e., not an array or null), you can use the Object.prototype.toString method.

JavaScript checks if an object

Simple example code: How to check whether a value is an object

instanceof by itself won’t work, because it misses two cases:

// oops:  isObject(Object.prototype) -> false
// oops:  isObject(Object.create(null)) -> false
function isObject(val) {
    return val instanceof Object; 
}

typeof x === 'object' won’t work, because of false positives (null) and false negatives (functions):

// oops: isObject(Object) -> false
function isObject(val) {
    return (typeof val === 'object');
}

Object.prototype.toString.call won’t work, because of false positives for all of the primitives:

> Object.prototype.toString.call(3)
"[object Number]"

> Object.prototype.toString.call(new Number(3))
"[object Number]"

So use:

function isObject(val) {
    if (val === null) { return false;}
    return ( (typeof val === 'function') || (typeof val === 'object') );
}

Or

function isObject(obj) {
  return obj === Object(obj);
}

Source: stackoverflow.com

Complete code

<!DOCTYPE html>
<html>
<body>

  <script>

   function isObject(obj) {
    return obj === Object(obj);
  }

  const test = {};
  if (isObject(test)){
    console.log("Test variable is object")
  }

</script>

</body>
</html> 

Output:

JavaScript check if object
  • Use the instanceof Function
const test = {};
function isObject(val) {
    return val instanceof Object; 
}
console.log(isObject(test));
  • Use the typeof() Function
const test = {};
function isObject(val) {
    return (typeof val === 'object');
}
console.log(isObject(test));
  • Use User-Defined Functions
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));
  • Use the getPrototypeOf()
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));

Or

function isObject(value) {
  return Object.prototype.toString.call(value) === '[object Object]';
}

// Examples
console.log(isObject({}));  // true
console.log(isObject([]));  // false
console.log(isObject(null));  // false
console.log(isObject('Hello'));  // false
console.log(isObject(123));  // false

In the above example, the isObject function checks if the value is an object by comparing the result of Object.prototype.toString.call(value) to the string '[object Object]'. If they match, it returns true, indicating that the value is a plain object.

Comment if you have any doubts or suggestions on this JS object topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published. Required fields are marked *