8

I need to know how to check the variable if its an Array or its an Object

var arr = ['foo', 'bar'];
var obj = {
  0: 'foo',
  1: 'bar'
}

document.write('arr is an: ' + typeof arr + ', obj is an: ' + typeof obj)

// The result is always:
// arr is an: object, obj is an: object

Is there any way to tell the difference between the two types?

0

1 Answer 1

2

Array.isArray(arr) will return true. Array.isArray(obj) will return false.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks that was really helpful

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.