Declare and Initialize arrays in JavaScript
This post will discuss how to declare and initialize arrays in JavaScript.
There are several ways to declare and initialize arrays in JavaScript. An array is a collection of values that can be accessed by an index. Here are some of the common functions:
1. Using an Array constructor
We can use the Array() constructor to create an array using the new keyword and the Array function. We can pass one or more values as arguments to the function. If only a single value is passed to the Array constructor, an empty array of that length is created, with all its elements undefined. However, if more than one values is passed to the Array constructor, the array is initialized with the given elements. The following example demonstrates this behavior:
|
1 2 3 4 5 |
let arr1 = new Array(5); console.log(arr1); // [<5 empty items>] let arr2 = new Array(1, 2, 3, 4, 5); console.log(arr2); // [1, 2, 3, 4, 5] |
To declare and initialize the array with the specified value, we can use the Array constructor with the fill() function. The fill() function updates all element in an array to a given value and returns the modified array.
|
1 2 3 4 5 |
let n = 5; let val = 0; let arr = Array(n).fill(val); console.log(arr); // [0, 0, 0, 0, 0] |
2. Using an array literal
An array literal is the easiest and most concise way to create an array. We can use square brackets [] to enclose a list of values separated by commas. For example:
|
1 2 3 4 5 6 7 |
// create an array of zero length let arr1 = []; console.log(arr1); // [] // initialize the array in a single line with desired elements let arr2 = [1, 2, 3, 4, 5]; console.log(arr2); // [1, 2, 3, 4, 5] |
3. Using Array.from() function
The Array.from() function creates a new array instance from the specified array and optionally map each array element to a new value using a mapping function. To create an array, we can pass an empty object with the length property defined. To initialize it with some value, map each element to a new value. For example:
|
1 2 3 4 5 6 7 8 9 10 11 |
let squares = Array.from([1, 2, 3, 4, 5], x => x * x); console.log(squares); // [1, 4, 9, 16, 25] let chars = Array.from("hello"); console.log(chars); // ["h", "e", "l", "l", "o"] let zeros = Array.from({ length: 5 }, () => 0); console.log(zeros); // [0, 0, 0, 0, 0] let mixed = Array.from({length: 3, 0: 221, 1: "B", 2: "Baker Street"}); console.log(mixed); // [221, 'B', 'Baker Street'] |
4. Using Array.of() function
The Array.of() static function that creates a new array from a variable number of arguments. It works similarly to the Array constructor, but it always creates an array with the given arguments as elements, regardless of their type or number. For example:
|
1 2 3 4 5 6 7 8 9 10 11 |
// an array with only one element let number = Array.of(5); console.log(number); // [5] // an array of numbers let numbers = Array.of(1, 2, 3, 4, 5); console.log(numbers); // [1, 2, 3, 4, 5] // an array of different types let mixed = Array.of(1, true, "221B Baker Street", null); console.log(mixed); // [1, true, "221B Baker Street", null] |
That’s all about declaring and initializing arrays in JavaScript. Keep in mind that we can use the Array.map() function to create a new array from an old array, with each item of the old array transformed to a new value. The map() function accepts a callback function as an argument and gives back a new array with the results of applying the function to every array element. For example:
|
1 2 |
let arr = [1, 2, 3, 4, 5].map(x => x * x); console.log(arr); // [1, 4, 9, 16, 25] |
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 :)