javascript

Check if given value is an array in Javascript

To check whether a given value is an array or not using javascript, Array.isArray() method can be used. It takes values as a parameter that need to be checked.

const arr = ['1', '2', '3'];
const str = 'Devsheet';

is_array = Array.isArray(arr);
console.log(is_array);

is_array = Array.isArray(str);
console.log(is_array);
Output
true
false

Arrays in javascript contain one or multiple items inside them and you can get array items using its index. 

In the code snippet, we have two variable arr and str that need to be checked whether they are array or not. When we execute Array.isArray(arr) it will return true and Array.isArray(str) will return false.

Live Demo

Array.isArray();
Array.isArray(null);
Array.isArray(true);
Array.isArray(undefined);
Array.isArray({});
Array.isArray(500);
The above lines of codes will return false when executed separately. As the value, we provided inside Array.isArray() is not treated as an array.
Array.isArray([]);
Array.isArray([NaN]);
Array.isArray([9])
Array.isArray(new Array());
Array.isArray(new Array('x', 'y', 'z'));
Array.isArray(new Array(3));
The lines of code above will return true when executed inside the javascript environment.
Was this helpful?