How do you check for an array?

To check if the value assigned to variable is an array, use Array.isArray() method.

let var1 = [‘red','yellow','green'];

Array.isArray(var1); // true

If the value is array, it will return true.

let var2 = {
  color1: 'red',
  color2: 'yellow',
  color3: 'green'

Array.isArray(var2); // false

if the value is not array, it will return false.

Remember array is a sub-type of an object. In our example, both var1 and var2 type is object.

typeof(var1); // "object"

typeof(var2); //"object"

Array.isArray() method will tell you if the passed object is an array as described above.