In this article, you’ll learn how to create and initialize arrays in different ways, understand the syntax and parameters, and discover why array literal syntax is often preferred.
It allows us to create new array objects by calling the Array() function with or without arguments.
You can create arrays in different ways, using the Array constructor.
Without any arguments:
When called without any arguments, the Array constructor creates an empty array with length 0.
let sampleArray = new Array();
With a specified length:
You can pass a single numeric argument to the constructor, which defines the length of the array.
let sampleArray = new Array(8);
You can pass multiple arguments to the constructor to initialize the array with specific elements.
let sampleArray = new Array(10, 20, 30);
The Array constructor provides additional methods that can be accessed through the created array object.
These methods allow you to perform operations like adding/removing elements, accessing specific elements, iterating over the array, and more.
It’s worth noting that using array literal syntax (e.g., ) is generally preferred over the Array constructor for creating arrays, as it is more concise and less error-prone.
new Array(); ✅ new Array(arrayLength); ✅ new Array(element0, element1, …, elementN); ✅
An integer between 0 and 2^32 – 1 (inclusive), represents the desired length of the new array.
If you give the Array constructor a single whole number between 0 and 2^32 – 1 (inclusive).
element0, element1, …, elementN
Here’s an example of how you might use the Array constructor:
let subjects = new Array('Math', 'Programming', 'English'); console.log(subjects);
In this example, an array of named subjects is created with three elements: “Math,” “Programming,” and “English.”
The first element in the array can be accessed with subjects, which outputs “Math.”
Example 1: Creating an empty array
let SampleArray1 = new Array(); console.log(arr1);
Example 2: Creating an array with a specified length
let SampleArray1 = new Array(); console.log(SampleArray1); let SampleArray2 = new Array(3); console.log(SampleArray2); console.log(SampleArray2.length);
[ <3 empty items> ] 3
Example 3: Creating an array with elements
let SampleArray3= new Array('Programming', 'Math', 'Science'); console.log(SampleArray3); ✅ console.log(SampleArray3); ✅
In these examples, SampleArray1 is an empty array, SampleArray2 is an array with a length of 3 but no elements, and SampleArray3 is an array with the elements.
[ 'Programming', 'Math', 'Science' ] Programming
The first element in SampleArray3 can be accessed with SampleArray, which outputs “Programming.”
It allows us to create arrays in different ways, either without any arguments, with a specified length, or with specific elements.
The Array constructor also provides additional methods that can be accessed through the created array object, enabling you to perform various operations like adding/removing elements, accessing specific elements, and iterating over the array.
Thank you for reading Itsourcecoders 😊.