Timeline for Check if a value is an object in JavaScript
Current License: CC BY-SA 4.0
47 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Mar 1, 2022 at 12:58 | comment | added | Bullsized |
simpified: isObject(field) { return typeof field === 'object' && field !== null; }
|
|
| Aug 29, 2021 at 22:20 | comment | added | Hunter Kohler |
This is wrong. Include typeof x == "function". Functions are objects.
|
|
| May 7, 2021 at 20:54 | comment | added | Dimitri Kopriwa |
a Date() will be detected with this.
|
|
| Mar 23, 2021 at 3:03 | comment | added | kuldeep chopra | we can check array : var yourVariable = [] typeof yourVariable === 'object' && yourVariable !== null && !Array.isArray(yourVariable) | |
| Sep 15, 2020 at 13:09 | history | edited | rap-2-h | CC BY-SA 4.0 |
added 81 characters in body
|
| Jun 1, 2020 at 21:44 | comment | added | Graham P Heath | How about deleting this answer then? There's no way to flag an answer as deprecated? | |
| Apr 27, 2020 at 12:48 | review | Suggested edits | |||
| Apr 27, 2020 at 22:40 | |||||
| Dec 26, 2019 at 18:22 | comment | added | Michael Krelin - hacker | @DonHatch, I guess confused are people who see the code and obviously it's non-obvious with this syntax. Not sure if it has any disastrous consequences tho. | |
| Dec 26, 2019 at 14:01 | comment | added | Don Hatch |
@RightSaidFred can you explain why you say typeof(foo) confuses people? I sometimes use it simply because, it seems to me, the question of whether typeof is an operator or function really doesn't matter in most cases, and I don't want to waste my time looking it up to remind myself. I don't think it's fair to say I'm confused; I'm just making things simple when they don't need to be complicated.
|
|
| Dec 26, 2019 at 1:28 | history | edited | ˈvɔlə | CC BY-SA 4.0 |
fixed typo
|
| S Nov 27, 2019 at 5:09 | history | suggested | shieldgenerator7 | CC BY-SA 4.0 |
Added link to the currently most upvoted answer
|
| Nov 26, 2019 at 23:08 | review | Suggested edits | |||
| S Nov 27, 2019 at 5:09 | |||||
| Nov 6, 2019 at 10:58 | history | edited | Victor Schröder | CC BY-SA 4.0 |
Makes important warning more proeminent. This is admittedly wrong answer, but keeps appearing at the first position of this highly ranked question.
|
| Jun 26, 2019 at 22:56 | comment | added | user985399 |
Never be specific is The winner pattern: Try using blabla and/or blabla something. EDIT: This answer gives an idea blabla ...
|
|
| Oct 15, 2018 at 6:21 | comment | added | Serhat Ates | good resource about null problem: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… | |
| Mar 11, 2018 at 8:22 | review | Suggested edits | |||
| Mar 11, 2018 at 9:09 | |||||
| Dec 28, 2017 at 17:17 | comment | added | doubleOrt |
Downvoted because you should at the very least mention typeof null == "object".
|
|
| Oct 20, 2017 at 16:32 | comment | added | Iran Reyes | Check out this article: tobyho.com/2011/01/28/checking-types-in-javascript | |
| Sep 4, 2017 at 3:48 | comment | added | pentaphobe | agree that this answer is dangerously incomplete (and thus wrong in context) Matt Fenwick's answer should be the accepted one | |
| Sep 1, 2017 at 2:24 | comment | added | user8202629 |
` (arr[index] instanceof Object || arr[index] instanceof Array)` === Array instanceof Object
|
|
| May 7, 2017 at 17:58 | review | Suggested edits | |||
| May 7, 2017 at 18:11 | |||||
| Dec 23, 2016 at 14:16 | comment | added | Andrew | This answer is bad because it doesn't tell you about the secret null problem in the original answer or the edit, because the answer doesn't tell you how to even use the typeof in the original answer or edit (what to compare to?), and because in the edit you wrote so much useless text that did not improve the answer. Vote down. | |
| Dec 21, 2016 at 15:22 | comment | added | CStff | "foo instanceof Object" can return true for functions and objects, so typeof is better. | |
| Aug 2, 2016 at 10:17 | history | rollback | Michael Krelin - hacker |
Rollback to Revision 3
|
|
| Jul 29, 2016 at 10:22 | history | edited | Jim G. | CC BY-SA 3.0 |
added 74 characters in body
|
| Jul 29, 2016 at 10:15 | history | edited | Jim G. | CC BY-SA 3.0 |
added 179 characters in body
|
| Jul 20, 2016 at 10:55 | comment | added | Michael Krelin - hacker | @maurice, true, that's what I do most of the time nowadays. Though I still hate it when people tell each other which one of the technically equivalent expression to use. I suppress the urge to do so when I have one (which happens a lot, admittedly:)). | |
| Jul 19, 2016 at 22:57 | comment | added | maurice |
@RightSaidFred excellent point. So for those who wish to use parentheses for grouping, it would be clearer to use the parentheses like (typeof something)==='object' instead of like typeof(something)==='object'.
|
|
| Jul 1, 2016 at 10:39 | history | edited | Michael Krelin - hacker | CC BY-SA 3.0 |
refer copypasters to the other answer.
|
| Jul 1, 2016 at 7:03 | review | Suggested edits | |||
| Jul 1, 2016 at 7:31 | |||||
| May 27, 2016 at 9:07 | comment | added | Lajos Mészáros | typeof [1, 2, 3] === 'object' | |
| Mar 4, 2016 at 22:41 | comment | added | Michael Krelin - hacker | @Andrew, read the next answer, the one that has more votes and perhaps deserves it. | |
| Mar 4, 2016 at 19:35 | comment | added | Andrew | can't add an extra sentence fragment explaining how to use typeof? :[ | |
| Jan 18, 2016 at 20:27 | comment | added | last-child | This should not be the accepted answer. Beyond the stylistic concerns raised by Jonathan, it is simply incorrect and does not bring up the very important subtleties in e.g. @matt-fenwick's answer. | |
| Sep 8, 2015 at 11:54 | comment | added | Michael Krelin - hacker | @Jonathan, there are better reasons for downvoting my answer, do you by chance have military background? :) | |
| Jun 19, 2015 at 14:52 | comment | added | Con Antonakos |
Arrays will also return as "objects" as in: someArray instanceof Object //true or typeof someArray === 'object' // true. What about: Object.prototype.toString.call(someObject) === "[object Object]", or "[object Array]" if you're trying to detect an array?
|
|
| Feb 9, 2015 at 23:07 | comment | added | Iran Reyes | Consider with this statement that the arrays are objects too, so if your code look like typeof(test) and test is an array then the result will be "object" | |
| Sep 15, 2014 at 19:14 | comment | added | Camilo Martin |
typeof null... object!
|
|
| Apr 8, 2014 at 21:12 | comment | added | Nikolai |
This answer is incorrect. typeof returns 'object' for null, which is not an object, and instanceof doesn't work for objects created using Object.create(null).
|
|
| Apr 6, 2014 at 18:11 | comment | added | Michael Krelin - hacker |
@maaartinus, it could as you can have function fntypeof(x){return typeof x}. Moreover, you can have function fninstanceof(x,y){return x instanceof y}. The reason why it's operator is probably because it is modelled after c sizeof that happens at compile tim. And the primary reason to use extensive parentheses in the code for me is that I don't always remember precedence ;)
|
|
| Apr 6, 2014 at 13:34 | comment | added | maaartinus |
@MichaelKrelin-hacker: For me it's clear... typeof x with parantheses looks like a function, but instanceof doesn't, no matter what you do. No idea why typeof is not a function (I don't know enough JS to decide if it could be).
|
|
| Jan 13, 2014 at 22:23 | review | Suggested edits | |||
| Jan 13, 2014 at 22:24 | |||||
| Sep 6, 2013 at 7:07 | review | Suggested edits | |||
| Sep 6, 2013 at 7:15 | |||||
| Dec 14, 2011 at 21:59 | comment | added | RightSaidFred |
@MichaelKrelin-hacker: I'll do my best. ;) But what about myvar instanceof(something) or (myvar)instanceof(something)? ;)
|
|
| Dec 14, 2011 at 20:55 | vote | accept | Danny Fox | ||
| Dec 14, 2011 at 20:48 | history | edited | JonH | CC BY-SA 3.0 |
Added hyperlink to instanceof
|
| Dec 14, 2011 at 20:39 | history | answered | Michael Krelin - hacker | CC BY-SA 3.0 |