Home
Node.js
Array Initialize
Updated Dec 13, 2023
Dot Net Perls
Initialize array. In programs we want to fill an array immediately with certain values. With Node.js, we can perform array initialization in many ways.
Array
For the simplest code, using an array literal to start is a good plan. We can have an empty array literal, or one with many elements in it.
Literal example. Here is an integer array with 3 initial elements. The initialization statement is clear and does not require many extra characters. This is ideal.
// Initialize array with 3 elements. var values = [10, 20, 30]; console.log("ARRAY: " + values);
ARRAY: 10,20,30
Constructor. We can use the Array constructor with the new keyword in Node. This is more complex. With one argument, we have a specified number of empty elements in our array.
Warning With one argument, we do not get a one-element Array. This specifies a capacity or initial length.
Info In a performance test, I found that an initial size can reduce array resizes and lead to better program performance.
// Create array with 3 empty elements. // ... Then assign them with indexes. var values = new Array(3); values[0] = 10; values[1] = 20; values[2] = 30; console.log("EXAMPLE: " + values);
EXAMPLE: 10,20,30
Constructor, many arguments. With 2 or more arguments, the Array constructor returns an array with those elements. This is the same as the array literal syntax.
// Use array constructor with 2 or more arguments to create an array. var values = new Array(10, 20, 30); console.log("VALUES: " + values);
VALUES: 10,20,30
Constructor, no new. The "new" keyword is not required with the Array constructor. It is acceptable to remove the "new" keyword as it is not required and makes the code longer.
// The new keyword is not required. var result = Array(3); console.log("RESULT: " + result);
RESULT: ,,
Empty literal. This is a good approach to creating an array. We create an empty array with an empty array literal. Then we push elements to it—we can even do this in a loop.
for
Detail We do not know all the elements immediately. This approach allows us to add elements as soon as we know what they are.
// Create an empty array and push elements to it. var values = []; values.push(10); values.push(20); values.push(30); console.log("ELEMENTS: " + values);
ELEMENTS: 10,20,30
Unshift. This is the worst approach to building an array. With unshift we add to the front of an array. But this is slow because after each call, all following elements are affected.
// Use unshift to initialize an array in reverse order. var values = []; values.unshift(30); values.unshift(20); values.unshift(10); console.log("RESULT: " + values);
RESULT: 10,20,30
Review. With square brackets, we create empty arrays and arrays with certain elements. This is an ideal syntax for initializing integer and string arrays in JavaScript.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Dec 13, 2023 (edit).
Home
Changes
© 2007-2025 Sam Allen