Sunday 2 September 2018

JavaScript: length of the array

‘arrayName.length’ property return number of elements in array.

array.html
<!DOCTYPE html>

<html>

<head>
    <title>Array</title>
</head>

<body>
    <script type="text/javascript">
        var array = [2, 3, 5, 7, 11, 13];

        function printArray(arr) {
            document.write("Elements in Array are : ");
            for (var i = 0; i < arr.length; i++) {
                document.write(arr[i] + " ");
            }
        }

        document.write("Length of array is " + array.length + "<br />");
        printArray(array);
    </script>
</body>

</html>

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

Length of array is 6
Elements in Array are : 2 3 5 7 11 13



Previous                                                 Next                                                 Home

No comments:

Post a Comment