Declare an empty array in JavaScript
This post will discuss how to declare an empty array in JavaScript.
An empty array is an array that has no elements in it. There are two main ways to declare an empty array in JavaScript: array literal notation and the Array constructor. Here are some examples of both functions:
1. Using an array literal
This is the simplest and most common way to create an empty array. We just use a pair of square brackets to indicate an array with no elements. Here’s an example:
|
1 2 3 4 5 |
// Using an array literal let myArray = []; // This creates an empty array console.log(myArray); // [] |
2. Using Array constructor
This is another way to create an empty array, but it is less concise and less preferred than the array literal function. We use the new keyword and the Array() constructor function to create an array object with no arguments. Here’s an example:
|
1 2 3 4 5 |
// Using the Array constructor let myArray = new Array(); // This also creates an empty array console.log(myArray); // [] |
Both functions will result in an array with a length of zero and no elements. However, there are some differences between them. For example, the array literal is more concise and readable, while the Array constructor can take arguments to specify the size or the elements of the array. Here’s an example:
|
1 2 3 4 5 |
// Using the Array constructor with a size argument let myArray = new Array(5); // This creates an array with 5 empty slots, which can be filled later console.log(myArray); // [ <5 empty items> ] |
Note that using the Array constructor with a single numeric argument will create an array with that many empty slots, not an array with that number as its only element. To create an array with a single number element, we need to use an array literal or wrap the number in another pair of brackets. Here’s an example:
|
1 2 3 4 5 6 7 8 9 |
// Using an array literal with a single numeric element let myArray1 = [5]; // This creates an array with 1 element: 5 console.log(myArray1); // [5] // Using the Array constructor with a single numeric element wrapped in brackets let myArray2 = new Array([5]); // This also creates an array with 1 element: 5 console.log(myArray2); // [[5]] |
In general, it is recommended to use the array literal function to declare an empty array in JavaScript, as it is simpler, faster, and less error-prone than the Array constructor function.
3. Using Array.of() or Array.from() function
The array literal and the Array constructor are two main ways to declare an empty array in JavaScript, as well as some variations. These variations are less common and may have some drawbacks, such as performance issues or confusion. Therefore, they are not recommended.
1. The Array.of() function creates an array from a variable number of arguments. If no arguments are given, it creates an empty array:
|
1 2 3 |
// an empty array let myArray = Array.of(); console.log(myArray); // [] |
2. The Array.from() function creates an array from an iterable or an array-like object. If the object is empty, it creates an empty array:
|
1 2 3 4 |
// an empty array let myArray = Array.from({}); console.log(myArray); // [] |
That’s all about declaring an empty array in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)