How do I check if a variable is an array in JavaScript?

To check if a variable is an array in JavaScript, use the following methods with examples:

1. Array.isArray() (Recommended)

The most reliable and modern way to check for arrays.
Syntax:

Array.isArray(variable);

Examples:

console.log(Array.isArray([1, 2, 3]));     // true
console.log(Array.isArray([]));            // true
console.log(Array.isArray({}));            // false
console.log(Array.isArray("Hello"));       // false
console.log(Array.isArray(null));          // false
console.log(Array.isArray(undefined));     // false

Legacy Browser Support (Polyfill):
For browsers older than IE9, add this polyfill:

if (!Array.isArray) {
  Array.isArray = function(arg) {
    return Object.prototype.toString.call(arg) === '[object Array]';
  };
}

2. instanceof Operator

Checks if the variable is an instance of Array.
Syntax:

variable instanceof Array;

Examples:

const arr = [1, 2, 3];
const notArr = {};

console.log(arr instanceof Array);     // true
console.log(notArr instanceof Array);  // false

Caveat:
Fails for arrays created in other execution contexts (e.g., cross-frame or cross-window):

// If an array is from another window/frame:
const iframe = document.createElement('iframe');
document.body.appendChild(iframe);
const frameArray = iframe.contentWindow.Array;
const crossFrameArr = new frameArray(1, 2, 3);

console.log(crossFrameArr instanceof Array); // false
console.log(Array.isArray(crossFrameArr));   // true (correct result)

3. Object.prototype.toString.call()

Uses the object’s internal [[Class]] to identify arrays.
Syntax:

Object.prototype.toString.call(variable) === '[object Array]';

Examples:

console.log(Object.prototype.toString.call([]) === '[object Array]'); // true
console.log(Object.prototype.toString.call({}) === '[object Array]'); // false

4. Check the constructor Property

Checks if the variable’s constructor is Array.
Syntax:

variable.constructor === Array;

Examples:

const arr = [];
const obj = {};

console.log(arr.constructor === Array);  // true
console.log(obj.constructor === Array);  // false

Caveats:

  • Fails if the variable is null or undefined (throws an error).
  • Unreliable if the constructor property is modified:
  const arr = [];
  arr.constructor = Object; // Tampering with the property
  console.log(arr.constructor === Array); // false (incorrect result)

5. Handling Array-like Objects

To distinguish arrays from array-like objects (e.g., arguments, NodeList):

function isArrayLike(variable) {
  return (
    variable != null &&
    typeof variable === 'object' &&
    'length' in variable &&
    !Array.isArray(variable) // Ensure it's not a real array
  );
}

console.log(isArrayLike(document.querySelectorAll('div'))); // true
console.log(isArrayLike([1, 2, 3])); // false (already an array)

Final Recommendation

Use Array.isArray() for most cases. It’s concise, reliable, and works across all modern environments. Avoid typeof (it returns "object" for arrays).

// Best practice
const myVar = [1, 2, 3];
if (Array.isArray(myVar)) {
  console.log("It's an array!");
}

Leave a Reply

Your email address will not be published. Required fields are marked *