Create empty array of a given size in JavaScript


To create an empty array of given size, use the new operator −

var numberArray = new Array(10);

After that, let’s set some values in the array. Following is the code −

Example

var numberArray = new Array(10);
console.log("The length="+numberArray.length)
numberArray=[10,20,30,40,50,60];
console.log("The array value=");
for(var i=0;i<numberArray.length;i++)
console.log(numberArray[i]);

To run the above program, you need to use the following command −

node fileName.js.

Here, my file name is demo52.js.

Output

This will produce the following output −

PS C:\Users\Amit\JavaScript-code> node demo52.js
The length=10
The array value=
10
20
30
40
50
60

Updated on: 03-Sep-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements