Skip to content

JavaScript dynamic array | Create Simple Dynamic Array example

  • by

How to Declare Dynamic Array in JavaScript?

You can declare Dynamic Array in JavaScript using 3 ways. Use any way to create a dynamic array. Your code should be inside the script tag to create it on the webpage loaded.

“JavaScript Dynamic Array means either increasing or decreasing the size of the array automatically.” In dynamic you can perform adding, removing elements based on index values.

Using literal

var array= ["A", "B", "C"];

Default constructor

var array= new Array();

Parameterized constructor

var array= new Array(1, 2, 3);

Create array dynamically in JavaScript Example code

HTML example code of JavaScript dynamic array:-

<!DOCTYPE html>
<html> 
<body>

    <script type="text/javascript">
        var arr = []; // declare 

        console.log(arr) // Null array

        for(var i=1; i <= 5; i++) {

         arr.push(i.toString());
     }
     console.log(arr);
 </script>
</body>
</html>

Output:

Create JavaScript dynamic array example

If element removed from an array then array size must be decreased and if an element added to an array then array size becomes increased.

Do comment if you have any doubts and suggestions on this topic or examples.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published. Required fields are marked *