Saturday 15 September 2018

JavaScript: Define array using Array constructor function

Till now we defined array using [], but JavaScript also provides a constructor function Array(), to define an array. It is available in two forms, one takes 1 argument and other takes 2 arguments.

Array(10) : Create an array of size 10 elements.

Array(10, 12) : Create an array with elements 10 and 12, [10, 12].

array.html
<!DOCTYPE html>

<html>

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

<body>
    <script type="text/javascript">
        var arr1 = new Array(10);
        var arr2 = new Array(10, 12);

        document.write("arr1 = " + arr1 + "<br />");
        document.write("arr2 = " + arr2 + "<br />");
    </script>
</body>

</html>

Open above page in browser, you can able to see following content.
arr1 = ,,,,,,,,,
arr2 = 10,12


Previous                                                 Next                                                 Home

No comments:

Post a Comment