A JavaScript object can be created using new keyword. It also allows to create object of array with initial values or you can assign the array element after defining array. It expects more than one elements as initial values, if single value provided, it will be considered as array length.
1 const <array_name> = new Array(<values>)
array_name : It is name of array which can be used to perform operation on array
values : A comma separated values to initialize an array but its optional, but it should be more than one element
Create array using new keyword and Array object
1
2
3
4 const values = new Array(10, 20, 30)
5
6
7 const strings = new Array("First", "Second", "Third")
8
9
10 const objs = new Array({id: 1}, {id: 2})
In the above example, we have defines array with different types and initial values but it is also possible to create array first and then assign values to an array. A values can be assign to the array by using array index which is starts from 0.
Create array and assign value to element
1
2
3
4 const values = new Array();
5
6 values[0] = 10
7 values[1] = 20
8
9
10 const strings = new Array();
11
12 strings[0] = "First";
13 strings[1] = "Second";
14
15
16 const objs = new Array();
17
18 objs[0] = {id: 1};
19 objs[1] = {id: 2};
Note : If you create array using new Array() constructor by specifying single value. It will consider it as length of array rather creating array with one element.
Array with single element
1 let array = new Array(3);
2
3
4 let array = [3];
Array of number literals
1
2 const values = new Array();
3
4 values[0] = 101;
5 values[1] = 202;
6 values[2] = 303;
7
8
9 for(num of values) {
10 console.log(num);
11 }
In the above example, an array object is create using new keyword and Array constructor and initialized each element with number literals. An array is iterated using for...of loop and print each elements.
Array of string literals
1
2 const strings = new Array();
3
4 strings[0] = "welcome";
5 strings[1] = "to";
6 strings[2] = "IOGyan";
7
8 for(str of strings) {
9 console.log(str);
10 }
In the above example, an array object is create using new keyword and Array constructor and initialized each element with string literals. An array is iterated using for...of loop and print each elements.
Array of object literals
1
2 const items = new Array();
3
4 items[0] = {id: 101, name: "Mobile", price: 1254.36};
5 items[1] = {id: 202, name: "Laptop", price: 2547.32};
6 items[2] = {id: 303, name: "Monitor", price: 2547.32};
7
8 for(item of items) {
9 for(key in item) {
10 console.log(key + " : " + item[key]);
11 }
12 }
In the above example, an array is create using new keyword and constructor method and initialized with object literals. An array is iterated using for...of that iterate each object literals. An object properties are iterated using for...in loop and prints each property with value separated by colon.
1 id : 101
2 name : Mobile
3 price : 1254.36
4 id : 202
5 name : Laptop
6 price : 2547.32
7 id : 303
8 name : Monitor
9 price : 2547.32
Related options for your search