JavaScript - Check if array
A JavaScript code example for checking if a given variable is an array.
The HTML has an output element with id "output" to display the check result.
<p id="output"></p>
The isArray()
method can be used to check if a value is an array.
The isArray()
method returns "true" if an array, "false" otherwise.
The isArray()
method has the following characteristics:
Empty array: true
Multidimensional array: true
Object: false
Array containing objects: false
let array = ['Apple', 'Banana', 'Grape'];
// Check if array
if (Array.isArray(array)) {
document.querySelector('#output').textContent = 'It is an array';
} else {
document.querySelector('#output').textContent = 'It is not an array';
}