JavaScript- Creating a Zero-Filled JS Array
Last Updated :
27 Dec, 2024
A zero-filled array is an array whose value at each index is zero. Below are the methods used for creating a Zero-Filled Array in JavaScript:
Using Array.prototype.fill() Method- Basic and Mostly Used
The fill() method is used to change the elements of the array to a static value from the initial index (by default 0) to the end index (by default array.length). The modified array is returned.
JavaScript
const arr = new Array(5).fill(0);
console.log(arr);
Array constructor takes the size of an array as a parameter if we pass one argument into it. fill() method takes the value we want to fill in the array.
Using Apply() and Map() Method
We have used the apply() method to create an array of 5 elements that can be filled with the map() method. In the map() method, we pass in a callback that returns 0 to fill all the indexes with zeros.
JavaScript
const arr = Array.apply(null, Array(5)).map(() => 0);
console.log(arr);
This method is used to create a new,shallow-copied array from an array-like or iterable object. The Array.from() is called which lets us map an empty array of size 5 to an array with some elements inside. The second argument is a callback that returns 0 which is used to fill the array.
JavaScript
const arr = Array.from(Array(5), () => 0)
console.log(arr);
The same result can be obtained by passing an object with the length property using the Array.from() method.
JavaScript
const arr = Array.from({
length: 5
}, () => 0)
console.log(arr);
Using for loop - Easy and Efficient Approach
JavaScript for loop is used to iterate the elements for a fixed number of times. JavaScript for loop is used if the number of the iteration is known.
JavaScript
let arr = []
for (i = 0; i < 5; i++) {
arr[i] = 0;
}
console.log(arr);
Using Lodash _.fill() Method - Third Party
The Lodash _.fill() method is used to fill a set of values into the array in a given range.
JavaScript
const len = 10; // Set the desired length of the array
const res = _.fill(Array(len), 0);
console.log(res);
Output:
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Similar Reads
Creating a Zero-Filled Array in PHP Creating an array filled with zeros can be useful in various scenarios, such as initializing an array for further manipulation or setting up a default state. This article explores multiple approaches to creating a zero-filled array in PHP. A zero-filled array is an array where each element is initia
3 min read
Create an Array of Given Size in JavaScript The basic method to create an array is by using the Array constructor. We can initialize an array of certain length just by passing a single integer argument to the JavaScript array constructor. This will create an array of the given size with undefined values.Syntaxconst arr = new Array( length );J
3 min read
Create an array filled with given values Using JavaScript In JavaScript, an array is a collection of values that can be stored and accessed in a single variable. Each value in an array is called an element, and elements can be of any data type, such as numbers, strings, or other objects. Arrays are indexed, meaning that each element has a numerical positio
5 min read
What is the most efficient way to create a zero filled array in JavaScript ? Creating zero-filled arrays is a common task in JavaScript, essential for initializing data structures, matrices, and more. This guide explores the most efficient methods to create zero-filled arrays, enhancing performance and code readability. Syntax: arr(length).fill(value);Importance of Zero-Fill
2 min read
JavaScript Array fill() Method The JavaScript Array fill() Method fills a given range of array elements with the given value. This method is used to manipulate the existing array according to our needs.Syntax:arr.fill(value,start,end)Parameters:Value: Value to be filled.Start: Start index (included) and its default value is 0.End
3 min read
JavaScript Array fill() Method The fill() method in JavaScript is used to fill all the elements of an array from a start index to an end index with a static value. It mutates the original array and returns the modified array. Syntax: arr.fill(value, start, end)Parameters: This method accepts three parameters as described below: P
3 min read