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?

Sooraj
  • 9,717
  • 9
  • 64
  • 99
Mustafa Dwaikat
  • 3,392
  • 9
  • 27
  • 41

1 Answers1

2

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

sahbeewah
  • 2,690
  • 1
  • 12
  • 18