JavaScript Tutorials

JavaScript Array isArray()
Syntax & Examples

isArray() static-method

The Array.isArray() method in JavaScript returns true if the argument is an array, or false otherwise.


Syntax of isArray()

The syntax of Array.isArray() static-method is:

Array.isArray(value)

This isArray() static-method of Array returns true if the argument is an array, or false otherwise.

Parameters

ParameterOptional/RequiredDescription
valuerequiredThe value to be checked.

Return Type

Array.isArray() returns value of type Boolean.



✐ Examples

1 Using Array.isArray() method to check if a value is an array

In JavaScript, we can use the Array.isArray() method to check if a value is an array.

For example,

  1. We define a variable arr with an array value [1, 2, 3].
  2. We use the Array.isArray() method with arr to check if it is an array.
  3. The result is stored in the variable isArray.
  4. We log isArray to the console using console.log() method to see if arr is an array.

JavaScript Program

const arr = [1, 2, 3];
const isArray = Array.isArray(arr);
console.log(isArray);

Output

true

2 Using Array.isArray() method to check if a value is not an array

We can use the Array.isArray() method to check if a value is not an array.

For example,

  1. We define a variable notArray with a non-array value 'hello'.
  2. We use the Array.isArray() method with notArray to check if it is not an array.
  3. The result is stored in the variable isArray.
  4. We log isArray to the console using console.log() method to see if notArray is an array.

JavaScript Program

const notArray = 'hello';
const isArray = Array.isArray(notArray);
console.log(isArray);

Output

false

3 Using Array.isArray() method to check if an object is an array

We can use the Array.isArray() method to check if an object is an array.

For example,

  1. We define a variable obj with an object value { key: 'value' }.
  2. We use the Array.isArray() method with obj to check if it is an array.
  3. The result is stored in the variable isArray.
  4. We log isArray to the console using console.log() method to see if obj is an array.

JavaScript Program

const obj = { key: 'value' };
const isArray = Array.isArray(obj);
console.log(isArray);

Output

false

Summary

In this JavaScript tutorial, we learned about isArray() static-method of Array: the syntax and few working examples with output and detailed explanation for each example.