In JavaScript how to detect weather the type is Array or Object?

509 Views Asked by At

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?

1

There are 1 best solutions below

1
On BEST ANSWER

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