Timeline for Check if a value is an object in JavaScript
Current License: CC BY-SA 4.0
19 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Apr 9, 2024 at 16:48 | comment | added | PHP Guru |
@Daan Can you please elaborate about the previous bug in V8 that affects Object(value)===value as a method for testing for objects? What is that bug exactly?
|
|
| Feb 6, 2023 at 21:43 | comment | added | Andrew |
This is the correct answer... It will capture objects, classes, functions, and arrays, but not null. All 4 of those are extensible via. Object.defineProperty() type functions, and that is what counts most.
|
|
| Jul 12, 2020 at 22:12 | comment | added | Netside | This is technically not vanilla javascript though, which this overflow focuses on -- it's code someone else essentially wrote in JS which won't work without Underscore being included. Maybe there should be overflows (is that even a term on here?) for the main frameworks and libraries like JQuery, Underscore, etc. | |
| Jan 15, 2020 at 14:21 | history | edited | Gust van de Wal | CC BY-SA 4.0 |
Formatting. The link to the updated version of underscore.js linked to the master branch, which could misalign the line anchors in the URL in future versions
|
| Jun 26, 2019 at 19:43 | comment | added | user985399 |
So now we can install Underscore.js and use _.isObject(x) && typeof x !== 'function' to check if x is an object and nothing else :P
|
|
| Mar 7, 2019 at 11:52 | history | edited | Daan | CC BY-SA 4.0 |
quote the lines of code precisely, instead of own version
|
| Feb 21, 2019 at 12:25 | history | edited | Daan | CC BY-SA 4.0 |
updated link + added updated way in underscore.js to determine object
|
| Nov 2, 2018 at 21:52 | comment | added | Domino |
I can't believe this ended up on page two. This is the quickest and most efficient way to make sure that using the in operator won't cause an error.
|
|
| Mar 4, 2017 at 9:58 | comment | added | Samuel Danielson | Nothing works for everything. Some objects are iterable and should be handled as arrays even though they are objects, depending on the application. | |
| Sep 4, 2016 at 20:30 | comment | added | Oriol | My answer is more complete but this is the only one among the others which works properly for all possible values. Hopefully the bounty sign will make this answer stand out and reach the top. | |
| Sep 4, 2016 at 20:21 | history | bounty awarded | Oriol | ||
| Jun 22, 2016 at 21:38 | comment | added | samvv |
@Daan That is why we have _.isPlainObject().
|
|
| Nov 17, 2015 at 20:59 | comment | added | tiffon |
Great answer. Handles null, too. Should be the accepted answer.
|
|
| Jun 16, 2015 at 12:00 | comment | added | Oriol |
This is the best answer. Object converts to an object, and === ensures obj is the same object.
|
|
| May 22, 2014 at 1:18 | comment | added | Ricky Boyce | @Nickolai ..and for iterating through nested objects. | |
| Apr 9, 2014 at 9:39 | comment | added | Daan | Because most of the time you want to distinguish an {} from a [] for example as input in a function | |
| Apr 8, 2014 at 21:17 | comment | added | Nikolai | why would you exclude an array? They are full-fledged objects. | |
| Jul 12, 2013 at 8:57 | comment | added | Daan |
In javascript an array is also an object, so most of the time you want to exclude the array: return obj === Object(obj) && Object.prototype.toString.call(obj) !== '[object Array]'
|
|
| Feb 5, 2013 at 11:50 | history | answered | Daan | CC BY-SA 3.0 |