← JavaScript Interview Questions

JavaScript Array Type

Write a function called `isArray(item)` that correctly detects a JavaScript array. The function will be used the following way.

const obj = {};
const str = '';
const num = 0;
const array = [];
const nothing = null;

isArray(obj); // false
isArray(str); // false
isArray(num); // false
isArray(nothing); // false
isArray(array); // true
1
1
Asked 5 years ago by Jam Risser

Answers

Incorrect Answer

function isArray(arr) {
  return typeof arr === 'array';
}

Correct Answer

function isArray(arr) {
  return Array.isArray(arr);
}
1
Answered 5 years ago by Jam Risser