Timeline for Check if a value is an object in JavaScript
Current License: CC BY-SA 4.0
45 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Apr 27, 2024 at 11:07 | history | edited | Géry Ogam | CC BY-SA 4.0 |
added 141 characters in body
|
| Apr 27, 2024 at 11:01 | history | edited | Géry Ogam | CC BY-SA 4.0 |
added 141 characters in body
|
| Nov 20, 2022 at 18:49 | comment | added | Juan |
const isObject = (value) => Object.prototype.toString.call(value) === '[object Object]';
|
|
| Oct 22, 2022 at 12:08 | comment | added | human | BUFFER is also an object.. so take care if you need to | |
| Sep 22, 2022 at 14:24 | comment | added | Scotty Jamison |
The formal definition of a JavaScript object includes functions, arrays, boxed primitives, and doesn't include null. To construct such a check, you can do (typeof obj === 'object' && obj !== null) || typeof obj === 'function'. Alternatively, this small piece of code will do the exact same thing: Object(obj) === obj. I would highly recommend you make your checks using this formal definition unless you have a good reason to deviate.
|
|
| Jun 1, 2022 at 15:01 | comment | added | felipou | It seems that much of the discussion around this question is because of the definition of what an "object" is. I came across this question looking for the following definition: something that will be serialized as an object when using JSON.stringify, and it seems that this is the best answer in that case. | |
| Sep 20, 2021 at 12:31 | history | edited | Naetmul | CC BY-SA 4.0 |
`typeof yourVariable === 'object'` implies `typeof yourVariable !== 'function'`
|
| S Sep 20, 2021 at 10:09 | history | suggested | theProCoder | CC BY-SA 4.0 |
fixed grammar (sorry, my own mistake on my first edit)
|
| Sep 20, 2021 at 3:18 | review | Suggested edits | |||
| S Sep 20, 2021 at 10:09 | |||||
| S Sep 19, 2021 at 19:41 | history | suggested | theProCoder | CC BY-SA 4.0 |
improved formatting and added check for functions.
|
| Sep 18, 2021 at 11:46 | review | Suggested edits | |||
| S Sep 19, 2021 at 19:41 | |||||
| S Jul 23, 2021 at 11:13 | history | suggested | Safin Ghoghabori | CC BY-SA 4.0 |
Array is also considered as Objects so i added code to exclude it.
|
| Jul 22, 2021 at 8:17 | review | Suggested edits | |||
| S Jul 23, 2021 at 11:13 | |||||
| May 29, 2021 at 4:21 | history | rollback | user229044♦ |
Rollback to Revision 5
|
|
| Apr 21, 2021 at 16:35 | comment | added | Vasiliy Rusin |
This doesn't work with Map() or other objects. U need use Object.prototype.toString.call(obj) if u want to check that object is Object
|
|
| Apr 21, 2021 at 3:36 | history | edited | dthree | CC BY-SA 4.0 |
added example to visualize
|
| Dec 4, 2019 at 20:22 | comment | added | TOPKAT |
Hi there are also other cases that are typeof 'object': Buffer, RegExp, Date
|
|
| Jun 26, 2019 at 23:24 | comment | added | user985399 |
This is the answer that worked for me to check if loaded module brings a declared (must be checked before) defined (not function or array or null but) object. The first comment everyone now can flag as not needed because it is answered by && yourVariable !== null. And also should be flaged is the accepted answer because handwaving is not an anser at all. Read it again and wake up: Try using typeof(var) and/or var instanceof something. EDIT: This answer gives an idea ... Sometimes answer is shosen because no else has answered.
|
|
| Jun 26, 2019 at 23:08 | comment | added | user985399 |
Everything is an Object so true will do - That is okay to say as much as Functions are also objects and should be included. But the programmer should have the freedom to be specific and exclude eventual functions sliped in! Or be able to sort out objects among functions! Or to have an argument that can be a function(){} closure or object {}.
|
|
| Jun 4, 2019 at 19:04 | comment | added | 3limin4t0r |
Normal objects are always considered to be truthy, while null is considered falsy. This means the second version could also be written as typeof yourVariable === 'object' && yourVariable
|
|
| S Nov 12, 2018 at 15:29 | history | suggested | Leonardo Ferreira | CC BY-SA 4.0 |
If you call yourVariable !== null in an undefined variable, the code will break. So you should test typeof yourVariable === 'object' first.
|
| Nov 12, 2018 at 14:19 | review | Suggested edits | |||
| S Nov 12, 2018 at 15:29 | |||||
| Dec 28, 2017 at 17:18 | comment | added | doubleOrt | Doesn't work on functions (@Daan's answer is obviously the best). | |
| Dec 28, 2017 at 17:09 | comment | added | doubleOrt |
@AlexanderFlenniken There is no need because typeof [] returns "object", arrays will be included in the check by default.
|
|
| Nov 29, 2017 at 22:34 | comment | added | Alexander Flenniken |
Downvoted because an array is also considered an object, so you should also check Array.isArray(yourVariable).
|
|
| Nov 7, 2017 at 4:23 | comment | added | Troy Watt |
The solution above does not take into account a RegExp or Date object, if you need to to support these object types you'll want to perform a check first. if( val && type val !== 'object && Object.prototype.toString.call(val) !== '[object RegExp]' && Object.prototype.toString.call(val) !== '[object Date]') {...}
|
|
| Oct 21, 2016 at 19:59 | comment | added | Urasquirrel | typeof [] is also "object" So how would we tell the difference without using instanceof which is rather slow. | |
| Oct 13, 2016 at 14:47 | comment | added | X_Trust |
yourVariable !== null && typeof yourVariable === 'object' && !Array.isArray( yourVariable ) Arrays type evaluates to "object". Added a check for arrays
|
|
| Feb 25, 2016 at 9:48 | comment | added | Jose Rui Santos |
@Tresdin The best way is to run Object.prototype.toString.call(yourVar), being yourVar what you need to inspect. In case of arrays, Object.prototype.toString.call([1,2]) returns [object Array]
|
|
| Jan 21, 2016 at 17:40 | comment | added | skud | This could also be used as an alternative for getting rid of null values for some cases: yourVariable = yourVariable || {}; if (typeof yourVariable === 'object') { // do something } | |
| Nov 8, 2015 at 1:36 | comment | added | Nick Steele | For noobs: JavaScript doesn't know what "If" means, it must be "if" (lowercase) or you'll get weird errors that will make you rip out your chest hairs | |
| Oct 28, 2015 at 10:45 | comment | added | James |
Would it not be more reliable to do yourVariable && typeof yourVariable === 'object rather than explicitly checking for null? What if yourVariable is undefined?
|
|
| Jul 6, 2015 at 22:38 | review | Suggested edits | |||
| Jul 7, 2015 at 0:13 | |||||
| Dec 1, 2014 at 15:48 | comment | added | Matt Fenwick |
@Orion the simple answer is that arrays are considered to be objects. For a more detailed answer, you'll have to read up on typeof because it has a few special cases that don't necessarily make a whole lot of sense. If you're trying to differentiate between arrays and objects which are not arrays, then you definitely don't want to use typeof.
|
|
| Aug 3, 2014 at 18:29 | comment | added | Lewis |
Does anybody know why typeof [1,2] is object ?
|
|
| Mar 18, 2014 at 21:02 | history | edited | Chuck | CC BY-SA 3.0 |
added 1 characters in body
|
| Sep 3, 2013 at 10:50 | comment | added | Konstantin Smolyanin |
@RightSaidFred Seems that typeof null == 'object' will not be fixed in ES6. They said: This proposal has been rejected. It was implemented in V8 but it turned out that it broke a lot of existing sites. In the spirit of One JavaScript this is not feasible.
|
|
| May 23, 2013 at 15:59 | comment | added | hippietrail |
In this case would yourVariable !== null be better practice?
|
|
| Dec 21, 2012 at 18:25 | comment | added | JS_Riddler | Functions are also objects and should be included in your check. | |
| Dec 14, 2011 at 22:28 | history | edited | Chuck | CC BY-SA 3.0 |
deleted 51 characters in body
|
| Dec 14, 2011 at 22:27 | comment | added | Chuck | @RightSaidFred: Wow, ES3 agrees. I always just trusted the typeof operator and figured it was an artifact of Java's influence on the object system. I was clearly wrong. | |
| Dec 14, 2011 at 21:53 | comment | added | RightSaidFred |
Yeah, I'm not sure what delnan meant by that, but ECMAScript 5 defines the null value as "primitive value that represents the intentional absence of any object value." in spite of the fact that its typeof operator claims otherwise. Not sure about the wording in ES3. This is set to be fixed in ES6. In any case, null doesn't behave in any way like an object. Your yourVariable != null is the common approach so +1.
|
|
| Dec 14, 2011 at 21:11 | history | edited | Chuck | CC BY-SA 3.0 |
added 161 characters in body
|
| Dec 14, 2011 at 21:08 | comment | added | Chuck | @RightSaidFred: Well, like delnan said, I guess that's kind of a question of definitions. I would actually be inclined to say null is an object based on the typeof operator's say-so. But I guess you're right that it's not the most useful definition. That's just my personal mental model of the language, where "is-object" and "is-null" are two separate flags. | |
| Dec 14, 2011 at 20:40 | history | answered | Chuck | CC BY-SA 3.0 |