Code Logo

Check Array with Array.isArray()

Published at25 Jul 2026
JavaScript Functions Easy 0 views
Like0

Write a JavaScript function that takes a value and returns true if it is an array using Array.isArray(), or false otherwise. Array.isArray() is the reliable way to check array types in JavaScript.

Array.isArray() is a static method that determines whether the passed value is an Array. Unlike typeof which returns "object" for arrays, Array.isArray() correctly distinguishes arrays from other objects. This is important because arrays are objects in JavaScript, and the typeof operator cannot differentiate between [] and {}.

Before Array.isArray() was introduced in ES5, developers used workarounds like Object.prototype.toString.call(value) === '[object Array]'. Array.isArray() is now the standard, reliable way to check for arrays. It works across different JavaScript contexts like iframes and windows where instanceof Array may fail due to differing Array constructors.

Time complexity is O(1) as Array.isArray() is a simple internal type check. Space complexity is O(1). The method does not modify the input value or perform any coercion — it returns true only for actual Array instances.

Edge cases include arrays (returns true), objects (returns false), null (returns false), undefined (returns false), strings (returns false), numbers (returns false), and typed arrays like Int8Array (returns false).

Example Input & Output

Example 1
Input
null
Output
false
Explanation

Null returns false

Example 2
Input
"hello"
Output
false
Explanation

String returns false

Example 3
Input
[1,2,3]
Output
true
Explanation

Array of numbers returns true

Example 4
Input
{}
Output
false
Explanation

Object literal returns false

Example 5
Input
[42]
Output
true
Explanation

Single-element array returns true

Algorithm Flow

Recommendation Algorithm Flow for Check Array with Array.isArray()
Recommendation Algorithm Flow for Check Array with Array.isArray()

Solution Approach

function solution(val) {
  return Array.isArray(val);
}

Best Answers

javascript - Approach 1
function solution(val) {
  return Array.isArray(val);
}