Top community members
All Wiki Articles Create Wiki Article

In the past we had a dream,
to have place where we could share IT knowledge,
to ask questions without fear that someone will judge us.

Now we are a group of people who make this dream come true. ❤ 💻

If you think sharing knowledge and helping other is valuable.

join our community - Click here

JavaScript - how to check if value is object?

0 contributions
14 points

The first important thing is what is primitive type in JavaScript?

Primitive type literals:

  • 012-4NaN, +Infinity, -Infinity, etc.
  • 'abc', '', etc.
  • true / false
  • null
  • undefined

Everything else is object type!

Below code shows how to check if value is object.

1. Custom method examples

1.1. With Object function

// ONLINE-RUNNER:browser;

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

// Usage example:

console.log( isObject(null)       ); // false
console.log( isObject(undefined)  ); // false
console.log( isObject(false)      ); // false
console.log( isObject(true)       ); // false
console.log( isObject(0)          ); // false
console.log( isObject(1)          ); // false
console.log( isObject(NaN)        ); // false
console.log( isObject(+Infinity)  ); // false
console.log( isObject('')         ); // false

console.log( isObject([ ])                 ); // true
console.log( isObject({ })                 ); // true
console.log( isObject(function(){ })       ); // true
console.log( isObject(new Number(1))       ); // true
console.log( isObject(Object.prototype)    ); // true
console.log( isObject(Object.create(null)) ); // true

1.2. With typeof operator

// ONLINE-RUNNER:browser;

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

// Usage example:

console.log( isObject(null)       ); // false
console.log( isObject(undefined)  ); // false
console.log( isObject(false)      ); // false
console.log( isObject(true)       ); // false
console.log( isObject(0)          ); // false
console.log( isObject(1)          ); // false
console.log( isObject(NaN)        ); // false
console.log( isObject(+Infinity)  ); // false
console.log( isObject('')         ); // false

console.log( isObject([ ])                 ); // true
console.log( isObject({ })                 ); // true
console.log( isObject(function(){ })       ); // true
console.log( isObject(new Number(1))       ); // true
console.log( isObject(Object.prototype)    ); // true
console.log( isObject(Object.create(null)) ); // true

 

0 contributions

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more