Updated March 27, 2023
Introduction to Dynamic Array in JavaScript
Dynamic Array in JavaScript means either increasing or decreasing the size of the array automatically. JavaScript is not typed dependent so there is no static array. JavaScript directly allows array as dynamic only. We can perform adding, removing elements based on index values. We no need to write extra logic to perform these operations. While an element removed from an array then array size must be decreased and if an element added to an array then array size becomes increased. Arrays are used to store homogenous elements means the same type of elements can be stored at a time. Example: We can store integer numbers, float numbers, double numbers, strings, characters, Objects, etc. but at a time any specific type only.
How to Declare Dynamic Array in JavaScript?
In Javascript, Dynamic Array can be declared in 3 ways:
1. By using literal
var array= ["Hi", "Hello", "How"];
2. By using the default constructor
var array= new Array();
3. By using parameterized constructor
var array= new Array("Hi", "Hello", "How");
How JavaScript Array Elements are Iterated?
Array elements are iterated by using:
- For loop
- While loop
- For Each loop
Advantage: Based on user requirement array size can be increased (stretched) or decreased(shrink) in JavaScript array.
How does Dynamic Array work in JavaScript?
Below the points explain how the array works in JavaScript:
It works based literal array and constructor array elements.
1: Literal Array
var array = [give some elements];
2: Default Constructor
var array=new Array();
3: Parameterized Constructor
var arra=new Array(Give some elements);
Array Functions in JavaScript
Below are the points explain the array function:
1. push(): push() function is used to add elements to the array.
Array.push(element)
2. length: length is used to get the size of the array.
Array.length
3. slice(): slice() function is used to remove the elements from an array based on the index.
Array.slice()
- Multi-dimensional Array in JavaScript: Array added as an item to another array in JavaScript makes array becomes multidimensional.
var a=new Array(new Array(elements), new Array(elements), new Array(elements),……);
- Can we assign an array to a variable directly in JavaScript: Yes, JavaScript allows assigning an array to a variable.
var a=[1,2,3,4]//if we write this syntax in Java it will shows compilation error.
Examples
The following are the examples to implement in JavaScript:
Example #1
Array literal with for loop.
Code:
//creating an array literal with static values
var staticArrayLiteral=[1,2,3,4,5,6,7,8,9,10];
//iterating array elements with for loop
for (var i = 0; i < staticArrayLiteral.length; i++) {
document.write("Array element of index staticArrayLiteral["+i+"] is :"+staticArrayLiteral[i]+"<br>");
}
Output:
Example #2
Array literal with the while loop.
Code:
//creating an array literal with static values
var staticArrayLiteral=[1.1,2.2,3.3,4.4,5.5,6.6,7.7,8.8,9.9,10.10,11.11];
//iterating array elements with while loop
var temp=0;
while (temp<staticArrayLiteral.length) {
document.write("Array element of index staticArrayLiteral["+temp+"] is :"+staticArrayLiteral[temp]+"<br>");
temp++;
}
Output:
Example #3
Array literal with the while loop.
Code:
//creating an array literal with static values
var staticArrayLiteral=["20-03-1995","2-2-2000","2-03-2001","2-12-2010","21-03-2000"];
//iterating array elements with forEach loop
staticArrayLiteral.forEach(element => document.write("Array elements "+element+"<br>"));
Output:
Example #4
Default constructor Array with for loop.
Code:
//creating a default Array constructor
var defaultConstructorArray=new Array();
//adding elements to default constructor with push() function
defaultConstructorArray.push("Ramesh");
defaultConstructorArray.push("Paramesh");
defaultConstructorArray.push("Venkatesh");
defaultConstructorArray.push("Amardeep");
defaultConstructorArray.push("Venkatachalam");
defaultConstructorArray.push("Sachin");
defaultConstructorArray.push("Mahesh");
defaultConstructorArray.push("Dravid");
//iterating array elements with for loop
for (var i = 0; i < defaultConstructorArray.length; i++) {
document.write("Array element of index defaultConstructorArray["+i+"] is :"+defaultConstructorArray[i]+"<br>");
}
Output:
Example #5
Default constructor Array with for loop
Code:
//creating a default Array constructor
var defaultConstructorArray=new Array();
//adding elements to default constructor with push() function
defaultConstructorArray.push(10);
defaultConstructorArray.push(20);
defaultConstructorArray.push(30);
defaultConstructorArray.push(40);
defaultConstructorArray.push(50);
defaultConstructorArray.push(60);
//iterating array elements with while loop
var temp=0;
while (temp < defaultConstructorArray.length) {
document.write("Array element of index defaultConstructorArray["+temp+"] is :"+defaultConstructorArray[temp]+"<br>");
temp++;
}
Output:
Example #6
Parameterized Array constructor with for loop.
Code:
//creating parameterized array constructor
var paramArrayConstructor=new Array('A','M','A','R','D','E','E','P');
//iterating array elements with for loop
for (var i = 0; i < paramArrayConstructor.length; i++) {
document.write("Array element of index paramArrayConstructor["+i+"] is :"+paramArrayConstructor[i]+"<br>");
}
Output:
Example #7
Parameterized Array constructor with for loop
Code:
//creating parameterized array constructor
var paramArrayConstructor=new Array('@','#','%','^','&','*','(',')','-','=','!');
//iterating array elements with while loop
var temp=0;
while (temp< paramArrayConstructor.length) {
document.write("Array element of index paramArrayConstructor["+temp+"] is :"+paramArrayConstructor[temp]+"<br>");
temp++;
}
Output:
Example #8
Assigning Array to a variable.
Code:
//creating an array literal and assign it to variable
var staticArrayLiteral=["Apple","Banana","Mango"];
document.write("=====Static Array Literal======="+"<br>");
//iterating array elements with forEach loop
staticArrayLiteral.forEach(element => document.write("Array elements "+element+"<br>"));
//creating a default Array constructor and assign it to variable
var defaultConstructorArray=new Array();
//adding elements to default constructor with push() function
defaultConstructorArray.push(101);
defaultConstructorArray.push(202);
defaultConstructorArray.push(303);
defaultConstructorArray.push(404);
document.write("=====default constructor Array ======="+"<br>");
//iterating array elements with for loop
for (var i = 0; i < defaultConstructorArray.length; i++) {
document.write("Array element of index defaultConstructorArray["+i+"] is :"+defaultConstructorArray[i]+"<br>");
}
//creating parameterized array constructor and assign it to variable
var paramArrayConstructor=new Array('@','#','%','^');
//iterating array elements with while loop
var temp=0;
document.write("=====Parameterized Constructor Array ======="+"<br>");
while (temp< paramArrayConstructor.length) {
document.write("Array element of index paramArrayConstructor["+temp+"] is :"+paramArrayConstructor[temp]+"<br>");
temp++;
}
Output:
Explanation: If in case of Java array values can’t be assigned to a variable directly but we have to assign it to an array only ([] array type) whereas in JavaScript we can assign the array to the variable.
Example #9
Two-dimensional array.
Code:
//creating two dimentional array
var a = new Array(new Array(1, 2, 3),new Array(4, 5, 6));
document.write("Two dimentional array elements are=>"+"<br>");
for(var i=0;i<2;i++)
{
for(var j=0;j<3;j++)
{
document.write(a[i][j]+"<br>");
}
}
Output:
Example #10
Removing the element with splice() function.
Code:
var array = ["Hi","Hello","How","are","you?"];
document.write("Array elements before removing are => <br>"+array+"<br>");
var indexValue = array.indexOf("How");
if (indexValue > -1) {
array.splice(indexValue, 1);
}
document.write("Array elements after removing are => <br>"+array);
Output:
Conclusion
JavaScript by default gives array as dynamic with predefined functions. Arrays in JavaScript can be created by using an array literal and Array constructor.
Recommended Articles
This is a guide to Dynamic Array in JavaScript. Here we discuss how Array elements literate, how to declare, functions, a constructor with examples to implement. You can also go through our other related articles to learn more –