JavaScript - isArray() method

revision:


Category : array

The Array.isArray() static method determines whether the passed value is an Array.

The isArray() method returns true if an object is an array, otherwise false. Array.isArray() is a static property of the JavaScript Array object.
You can only use it as Array.isArray(). Using x.isArray(), where x is an array will return "undefined".

Syntax :

        Array.isArray(value)      
        Array.isArray(obj)      
    

Parameters:

value, object : required; the value to be checked, or an object (or any data type) to be tested.

Examples:

            console.log(Array.isArray([1, 3, 5]));
            // Expected output: true
            console.log(Array.isArray('[]'));
            // Expected output: false
            console.log(Array.isArray(new Array(5)));
            // Expected output: true
            console.log(Array.isArray(new Int16Array([15, 33])));
            // Expected output: false
        

Practical examples

example: using Array.isArray() method.

see console.log()

code:
                    <div>
                        
                    </div>
                    <script>
                        // all following calls return true
                        console.log(Array.isArray([]));
                        console.log(Array.isArray([1]));
                        console.log(Array.isArray(new Array()));
                        console.log(Array.isArray(new Array("a", "b", "c", "d")));
                        console.log(Array.isArray(new Array(3)));
                        // Little known fact: console.log(Array.prototype itself is an array:
                        console.log(Array.isArray(Array.prototype));
            
                        // all following calls return false
                        console.log(Array.isArray());
                        console.log(Array.isArray({}));
                        console.log(Array.isArray(null));
                        console.log(Array.isArray(undefined));
                        console.log(Array.isArray(17));
                        console.log(Array.isArray("Array"));
                        console.log(Array.isArray(true));
                        console.log(Array.isArray(false));
                        console.log(Array.isArray(new Uint8Array(32)));
                        // This is not an array, because it was not created using the
                        // array literal syntax or the Array constructor
                        console.log(Array.isArray({ __proto__: Array.prototype }));
                    </script>     
                

example: check if an object is an array.

Array.isArray() returns true if an object is an arry, otherwise false:

code:
                    <div>
                        <p>Array.isArray() returns true if an object is an arry, otherwise false:</p>
                        <p id="array-1"></p>
                    </div>
                    <script>
                        const fruits = ["Banana", "Orange", "Apple", "Mango"];
                        let result =  Array.isArray(fruits);
                        document.getElementById("array-1").innerHTML = "is [fruits] an array? " + result;
                    </script>
                

example: check if another data type is an array.

Array.isArray() returns true if a datatype is an arry, otherwise false:

code:
                    <div>
                        <p>Array.isArray() returns true if a datatype is an arry, otherwise false:</p>
                        <p id="array-2"></p>
                    </div>
                    <script>
                        let text = "W3Schools";
                        let result1 =  Array.isArray(text);
                        document.getElementById("array-2").innerHTML = "is text an array? " + result1;
                    </script>