Sunday 30 September 2018

JavaScript: Arrays: isArray: Check whether reference is array or not

‘isArray’ method return true, if the passed value is an Array, else false.

Syntax
Array.isArray(obj)

isArray.html
<!DOCTYPE html>

<html>

<head>
    <title>isArray example</title>
</head>

<body>
    <script type="text/javascript">
        var ele1 = [];
        var ele2 = [2];
        var ele3 = 10;
        var ele4 = 10.23;
        var ele5 = "Hello";
        var ele6 = {};
        var ele7 = function(x) {
            return x * x;
        }

        document.write("Array.isArray(ele1) : " + Array.isArray(ele1) + "<br />");
        document.write("Array.isArray(ele2) : " + Array.isArray(ele2) + "<br />");
        document.write("Array.isArray(ele3) : " + Array.isArray(ele3) + "<br />");
        document.write("Array.isArray(ele4) : " + Array.isArray(ele4) + "<br />");
        document.write("Array.isArray(ele5) : " + Array.isArray(ele5) + "<br />");
        document.write("Array.isArray(ele6) : " + Array.isArray(ele6) + "<br />");
        document.write("Array.isArray(ele7) : " + Array.isArray(ele7) + "<br />");
    </script>
</body>

</html>

Open above page in browser, you can able to see following text.

Array.isArray(ele1) : true
Array.isArray(ele2) : true
Array.isArray(ele3) : false
Array.isArray(ele4) : false
Array.isArray(ele5) : false
Array.isArray(ele6) : false
Array.isArray(ele7) : false


Previous                                                 Next                                                 Home

No comments:

Post a Comment