Checking for an array in JavaScript

There is a special method Array.isArray that can be used to check whether a variable contains a real array or not. Let's test the method.

An array:

let test = [1, 2, 3]; let res = Array.isArray(test); console.log(res); // shows true

An object:

let test = {a: 1, b: 2, c: 3}; let res = Array.isArray(test); console.log(res); // shows false

A primitive:

let test = 'abcde'; let res = Array.isArray(test); console.log(res); // shows false

A pseudo-array:

let test = document.querySelectorAll('p'); let res = Array.isArray(test); console.log(res); // shows false

Given a two-dimensional array:

let test = [ [1, 2, 3], {a: 1, b: 2, c: 3}, [3, 4, 5], {x: 1, y: 2, z: 3}, ];

Loop through this array and for each element check if it is an array or not.

enru