# check variable is object

**URL:** <https://es.discourse.group/t/check-variable-is-object/1272>\
**Category:** 💡 Ideas\
**Tags:** proposal\
**Created:** [April 3, 2022, 10:19am UTC](https://es.discourse.group/t/check-variable-is-object/1272 "2022-04-03T10:19:44Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![MuratovBektur](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/muratovbektur/32/1314_2.png) [@MuratovBektur](https://es.discourse.group/u/MuratovBektur)\
**Post date:** [April 3, 2022, 10:19am UTC](https://es.discourse.group/t/check-variable-is-object/1272/1 "2022-04-03T10:19:44Z")

</div>

sorry for my english

sometimes you need to check if the variable is a pure object (not an array or function) and so I suggest adding a method for the Object class:

**Object.isObject**

```javascript
if (!Object.isObject) {
  Object.isObject = function (i) {
	return Boolean(i) &&
      typeof i === 'object' &&
      !Array.isArray(i)
  }
}

```

---

<div class="post-metadata">

**Author:** ![aclaymore](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/aclaymore/32/501_2.png) [@aclaymore](https://es.discourse.group/u/aclaymore)\
**Post date:** [April 3, 2022, 1:24pm UTC](https://es.discourse.group/t/check-variable-is-object/1272/2 "2022-04-03T13:24:55Z")

</div>

Welcome @MuratovBektur !

Do you think it could be confusing to say that arrays and functions are not objects, when both _inherit_ from `Object` ?

Maybe you could provide an example of when this check would be useful?

---

<div class="post-metadata">

**Author:** ![aclaymore](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/aclaymore/32/501_2.png) [@aclaymore](https://es.discourse.group/u/aclaymore)\
**Post date:** [April 3, 2022, 1:31pm UTC](https://es.discourse.group/t/check-variable-is-object/1272/3 "2022-04-03T13:31:38Z")

</div>

Also by the way, to be safe `Array.isArray` should be wrapped in a try catch as it can throw an exception.

```javascript
let {proxy, revoke} = Proxy.revocable([], {});
Array.isArray(proxy); // true
revoke();
Array.isArray(proxy); // TypeError!

```

---

<div class="post-metadata">

**Author:** ![MuratovBektur](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/muratovbektur/32/1314_2.png) [@MuratovBektur](https://es.discourse.group/u/MuratovBektur)\
**Post date:** [April 3, 2022, 2:07pm UTC](https://es.discourse.group/t/check-variable-is-object/1272/4 "2022-04-03T14:07:39Z")

</div>

I'll ask you a counter question, maybe you could provide an example of when Array.isArray check would be useful?

---

<div class="post-metadata">

**Author:** ![MuratovBektur](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/muratovbektur/32/1314_2.png) [@MuratovBektur](https://es.discourse.group/u/MuratovBektur)\
**Post date:** [April 3, 2022, 2:09pm UTC](https://es.discourse.group/t/check-variable-is-object/1272/5 "2022-04-03T14:09:59Z")

</div>

Js is a dynamic language. From time to time we must check type.

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [April 3, 2022, 2:18pm UTC](https://es.discourse.group/t/check-variable-is-object/1272/6 "2022-04-03T14:18:55Z")

</div>

It’s often useful when you want to use array methods and know that a value is a conceptual list.

---

<div class="post-metadata">

**Author:** ![MuratovBektur](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/muratovbektur/32/1314_2.png) [@MuratovBektur](https://es.discourse.group/u/MuratovBektur)\
**Post date:** [April 3, 2022, 2:22pm UTC](https://es.discourse.group/t/check-variable-is-object/1272/7 "2022-04-03T14:22:07Z")

</div>

i could say the same about Object

---

<div class="post-metadata">

**Author:** ![MuratovBektur](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/muratovbektur/32/1314_2.png) [@MuratovBektur](https://es.discourse.group/u/MuratovBektur)\
**Post date:** [April 3, 2022, 2:27pm UTC](https://es.discourse.group/t/check-variable-is-object/1272/8 "2022-04-03T14:27:10Z")

</div>

at the moment we can check all basic types by native methods except object

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [April 3, 2022, 2:34pm UTC](https://es.discourse.group/t/check-variable-is-object/1272/9 "2022-04-03T14:34:21Z")

</div>

sure. And you can check that something is a non-array non-function object all you like, but generally it’s easiest to just assume that if it’s an object, it can be used like one, without any “is plain object” check.

---

<div class="post-metadata">

**Author:** ![aclaymore](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/aclaymore/32/501_2.png) [@aclaymore](https://es.discourse.group/u/aclaymore)\
**Post date:** [April 3, 2022, 2:38pm UTC](https://es.discourse.group/t/check-variable-is-object/1272/10 "2022-04-03T14:38:03Z")

</div>

Hi @MuratovBektur !

The difference from `Array.isArray`, is that check narrows down the type considerably. Knowing something is an object but not an array or function has not narrowed down the type by much, it could still be a `Map`, `Set`, `ArrayBuffer` or `Object.create(null)`.

---

<div class="post-metadata">

**Author:** ![MuratovBektur](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/muratovbektur/32/1314_2.png) [@MuratovBektur](https://es.discourse.group/u/MuratovBektur)\
**Post date:** [April 3, 2022, 2:41pm UTC](https://es.discourse.group/t/check-variable-is-object/1272/11 "2022-04-03T14:41:02Z")

</div>

but it is better to be sure that this object is an object and not an array or anything else. I think people use Array.isArray, typeof v = = = 'string' and other type checking methods to make sure the code doesn't break. What is the object so deprived of?

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [April 3, 2022, 2:43pm UTC](https://es.discourse.group/t/check-variable-is-object/1272/12 "2022-04-03T14:43:57Z")

</div>

I’m not sure what you mean. There’s typeof, and Array.isArray, and that’s basically it for a direct reliable test. (there’s indirect means, which [https://npmjs.com/~inspect-js](https://npmjs.com/~inspect-js) uses for all its “is-“ predicates.

---

<div class="post-metadata">

**Author:** ![MuratovBektur](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/muratovbektur/32/1314_2.png) [@MuratovBektur](https://es.discourse.group/u/MuratovBektur)\
**Post date:** [April 3, 2022, 2:44pm UTC](https://es.discourse.group/t/check-variable-is-object/1272/13 "2022-04-03T14:44:36Z")

</div>

if you only knew how often newcomers are wrong when checking an object for an object, when checking an array or null that they are an object

---

<div class="post-metadata">

**Author:** ![MuratovBektur](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/muratovbektur/32/1314_2.png) [@MuratovBektur](https://es.discourse.group/u/MuratovBektur)\
**Post date:** [April 3, 2022, 2:54pm UTC](https://es.discourse.group/t/check-variable-is-object/1272/14 "2022-04-03T14:54:55Z")

</div>

previously, arr.indexOf (el)! = = -1 was used to verify the presence of an element in the array, but then arr.includes (el) was added and the code became much clearer. And I would like a more understandable code for checking an object for objects. You ask why check the type of variable on the object, but please watch how many times people on the Internet look for [Check if a value is an object in JavaScript](https://stackoverflow.com/questions/8511281/check-if-a-value-is-an-object-in-javascript)(Viewed 1.7m times)

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [April 3, 2022, 2:55pm UTC](https://es.discourse.group/t/check-variable-is-object/1272/15 "2022-04-03T14:55:42Z")

</div>

The problem is that “object but not array” isn’t a type, it’s an arbitrary subset of a type. Typeof can be used to check for objects; arrays are objects.

---

<div class="post-metadata">

**Author:** ![bergus](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/bergus/32/152_2.png) [@bergus](https://es.discourse.group/u/bergus)\
**Post date:** [April 3, 2022, 3:00pm UTC](https://es.discourse.group/t/check-variable-is-object/1272/16 "2022-04-03T15:00:42Z")

</div>

> [@MuratovBektur](#):
>
> if you only knew how often newcomers are wrong when checking an object for an object

If anything, `Object.isObject` should then be implemented using

```js
typeof x == 'object' && x !== null || typeof x == 'function'

```

or using

```js
Object(x) === x

```

So basically `Object.isNotPrimitive`. It shouldn't special-case any specific kind of object like array objects, function objects, primitive wrapper objects, regex objects, etc.

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/senocular/32/642_2.png) [@senocular](https://es.discourse.group/u/senocular)\
**Post date:** [April 3, 2022, 3:07pm UTC](https://es.discourse.group/t/check-variable-is-object/1272/17 "2022-04-03T15:07:04Z")

</div>

I could maybe see some value in a utility like this when it comes to JSON? What kinds of values result in `{...}` in JSON? Arrays don't. Functions don't. Just about any other object does. Dates don't, but they implement their own toJSON and, well, I don't think there's much you can do about that since you don't know what's coming out of that (for any arbitrary object that implements it) unless you call it.

There have also been times where I would have liked to know if an object was an ordinary object or not - something not exotic or with any hidden internal slots/methods that could cause unexpected behavior, irrespective of the prototype chain.

```javascript
{} // yes
Object.create(null) // yes
Object.setPrototypeOf([], Object.prototype) // no (exotic array)
Object.setPrototypeOf(new String, Object.prototype) // no ([[StringData]] slot)

```

though I don't recall if there was any practical use for wanting this information or if it was more about curiosity. I suspect the latter.

...though the new String object above is also interesting for the JSON use case since stringify explicitly checks for primitive slots (like [[StringData]]) in objects for conversions. I guess they would also not be considered objects by that definition either.

```javascript
JSON.stringify(new String('hello')) // "hello"

```

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [April 3, 2022, 3:29pm UTC](https://es.discourse.group/t/check-variable-is-object/1272/18 "2022-04-03T15:29:02Z")

</div>

That’s the problem - unfortunately, the way JS works is that (almost) everything inherits from Object (and null objects don’t inherit from anything). This means that answering the question “is this an object” doesn’t have an answer that’s simultaneously universal/generic, and also useful.

Each individual use case has to answer that themselves, and it wouldn’t make sense for the language to codify just one, or all, of those dozens of rubrics. Thus, things are indeed confusing here for newcomers, and that is both unavoidable and unfixable, due to the nature of the language.

---

<div class="post-metadata">

**Author:** ![theScottyJam](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/thescottyjam/32/616_2.png) [@theScottyJam](https://es.discourse.group/u/theScottyJam)\
**Post date:** [April 3, 2022, 6:44pm UTC](https://es.discourse.group/t/check-variable-is-object/1272/19 "2022-04-03T18:44:35Z")

</div>

Yeah, so perhaps here we need something a little more concrete than "is this an object", because what @bergus gave would be the canonical way to answer that.

> [@bergus](#):
>
> `typeof x == 'object' && x !== null || typeof x == 'function'`

However, this definition will include arrays and what-not, because those _are_ objects, so it makes sense. If we don't include arrays, should we include TypedArray? If we don't include TypedArray should we include userland array classes? If we don't include userland array classes, well, how do avoid doing that?

Perhaps, what you're looking for is just a plain, simple object, the kind of object that you get with an object literal. If it's an instance of any class, you want `Object.isObject()` to return false. In other words, perhaps you want something like this:

```javascript
> const isObject = value => Object.getPrototypeOf(value) === Object.prototype
> isObject({ x: 2 })
true
> isObject([])
false

```

We probably shouldn't call a function like this `isObject()`, since it's technically returning false for values that really are objects.

And, this will return `false` for objects that inherit from the `null` prototype, which is unfortunate. We could consider making a special case for that as well.

---

<div class="post-metadata">

**Author:** ![josh](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/josh/32/1374_2.png) [@josh](https://es.discourse.group/u/josh)\
**Post date:** [May 17, 2022, 8:13am UTC](https://es.discourse.group/t/check-variable-is-object/1272/20 "2022-05-17T08:13:59Z")

</div>

In 2011 there was a discussion about adding an `Object.isObject` method, [harmony:typeof\_null [ES Wiki]](https://web.archive.org/web/20160331031419/http://wiki.ecmascript.org:80/doku.php?id=harmony:typeof_null#discussion_of_older_object.isobject_strawman), which which was primarily focused on fixing `typeof null == 'object'` semantics, but also should provide some useful context to this proposal thread.

[Next page](https://es.discourse.group/t/check-variable-is-object/1272.md?page=2)
