-1

typeof foo will just evaluate to 'object' whether foo is a list [] or a dictionary {} or any kind of object. How can one tell the difference?

apc518
  • 193
  • 1
  • 4
  • 12
  • 1
    [`Array.isArray`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray) – Eldar Dec 29 '21 at 11:59
  • 2
    It has been answered numerous times – charlietfl Dec 29 '21 at 12:04
  • when I search this question, nothing comes up. It doesn't matter if the question was answered 12 years ago if the post where it was answered doesn't show up on a search. The question that this one is marked as a duplicate of, asks with the word "array", so maybe thats why, but of course javascript has two different kinds of arrays so its not clear even then – apc518 Dec 29 '21 at 12:06
  • Are you saying that none of the other answers are valid because you asked about "list" and not "array"? If you didn't find something in a search does that mean it doesn't exist? Of course not. The message above simply states : *"This question already has answers here: "* – charlietfl Dec 29 '21 at 12:18
  • no, the other one is definitely valid and answers my question perfectly. I'm saying that right now, if you search using the term "list" you will likely not find the answer. And since javascript has two kinds of arrays, using the term "list" seems pretty likely for a lot of people, so a lot of people will not find the answer. I didn't, thats why I asked here. – apc518 Dec 29 '21 at 12:24
  • Float32Array, Uint8Array, etc, vs []. Typed arrays vs what I've been calling "lists" – apc518 Dec 29 '21 at 13:03
  • @apc518 they are still arrays. No such concept as "list" by defaulr – Manos Kounelakis Dec 29 '21 at 15:19
  • thats great, but not everyone knows that, including myself until now. My point is, if someone searches for this problem with the term "list", they will probably not find the answer, which is a very reasonable thing to do seeing as other languages call their variable-length ordered O(1) addressable iterable collections "lists" (e.g. python) – apc518 Dec 29 '21 at 23:52
  • So if they search term "list" then this question could turn up and the link above will take them to solutions – charlietfl Dec 29 '21 at 23:54
  • It is what it is.....someone probably decided it didn't show research effort – charlietfl Dec 29 '21 at 23:57

1 Answers1

1

In Javascript, arrays are objects, as is the value null. You simply use the Array.isArray method to see if anything is one.

const obj = {}
const arr = []

Array.isArray(obj) // false
Array.isArray(arr) // true
Steven Spungin
  • 27,002
  • 5
  • 88
  • 78