-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?

10
  • 1
    Array.isArray Commented Dec 29, 2021 at 11:59
  • 2
    It has been answered numerous times Commented Dec 29, 2021 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 Commented Dec 29, 2021 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: " Commented Dec 29, 2021 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. Commented Dec 29, 2021 at 12:24

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.