JavaScript Array.isArray()

Posted by

JavaScript Array.isArray()

The Array.isArray() method in JavaScript is used to determine whether the given value is an array or not. The Array.isArray() method returns a Boolean value which indicates whether the argument is an array or not.

Syntax

The syntax of the Array.isArray() method is given below.

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

Array.isArray(obj)

[/dm_code_snippet]

Parameters

The Array.isArray() method takes a single parameter as mentioned above and described below.

  • obj – The parameter obj is the value to be determined whether it is an array or not.

Return value

The Array.isArray() method returns a Boolean value which indicates whether the argument is an array or not.

true – If the argument is an array.

false – If the argument is not an array.

JavaScript Array.isArray() Example

The following example shows the usage of the Array.isArray() method.

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

let array1 = [1, 2, 3, 4, 5];
let array2 = 'Hello World';

console.log(Array.isArray(array1));
console.log(Array.isArray(array2));

[/dm_code_snippet]

The above code will produce the following output.

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

true 
false

[/dm_code_snippet]

The first console.log statement returns true as the array1 is an array and the second console.log statement returns false as the array2 is not an array.

Browser Support

The Array.isArray() method is supported in all major browsers such as Google Chrome, Firefox, Internet Explorer, Safari, and Opera.