JavaScript Program to Create an Array with a Specific Length and Pre-filled Values
Last Updated :
16 Jul, 2024
In JavaScript, we can create an array with a specific length and pre-filled values using various approaches. This can be useful when we need to initialize an array with default or placeholder values before populating it with actual data.
The Array constructor can be used to create an array of a specific length. The fill() method can fill this array with a specified value.
Syntax:
const filledArray = new Array(length).fill(value);
Example: Below is the implementation of the above approach
JavaScript
const length = 5;
const value = 5;
const filledArray = new Array(length).fill(value);
console.log(filledArray);
Method 2: Using a Loop to Initialize Values
We can use a loop to iterate through the desired length and assign values.
Syntax:
const filledArray = Array.from();
for (let i = 0; i < length; i++) {
filledArray.push(value);
}
Example: Below is the implementation of the above approach
JavaScript
const length = 5;
const filledArray = new Array();
for (let i = 0; i < length; i++) {
filledArray.push(i + 1);
}
console.log(filledArray);
Method 3: Using the map() Method
The map() method can be used to create an array by applying a function to each element.
Syntax:
const filledArray = Array.from({ length }).map(() => value);
Example: Below is the implementation of the above approach
JavaScript
const length = 5;
const filledArray =
Array.from({ length })
.map(() => 5);
console.log(filledArray);
Using Array.from with a Mapping Function
Using Array.from with a mapping function, you can create an array of a specific length with pre-filled values. The mapping function defines the values to fill the array. This approach offers concise and flexible syntax for generating arrays with custom initial values.
Example: In this example we generates a new array filledArray of length 5, filled with the value 0 using Array.from() and an arrow function. It then logs filledArray to the console.
JavaScript
const length = 5;
const value = 0;
const filledArray = Array.from({ length }, () => value);
console.log(filledArray);
Method 5: Using the Spread Operator
In this approach, we use the spread operator (...) to expand an array and then use the Array.prototype.map method to fill it with a specific value. This method provides a concise and flexible way to create an array with pre-filled values.
Syntax:
const filledArray = [...Array(length)].map(() => value);
Example:
JavaScript
const length = 5;
const value = 0;
const filledArray = [...Array(length)].map(() => value);
console.log(filledArray);
Similar Reads
Create an Array of Specific Length with Pre-filled Values Creating an array of a specific length with pre-filled values is a common task in many programming languages. This operation is useful for initializing arrays with default values, setting up initial states, or preparing data structures for further manipulation. These types of problem in PHP are used
2 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
How to Declare an ArrayList with Values in Java? ArrayList is simply known as a resizable array. Declaring an ArrayList with values is a basic task that involves the initialization of a list with specific elements. It is said to be dynamic as the size of it can be changed. Proceeding with the declaration of ArrayList, we have to be aware of the co
2 min read
Java Program to Print the Elements of an Array An array is a data structure that stores a collection of like-typed variables in contiguous memory allocation. Once created, the size of an array in Java cannot be changed. It's important to note that arrays in Java function differently than they do in C/C++As you see, the array of size 9 holds elem
6 min read
How to Find Length or Size of an Array in Java? Finding the length of an array is a very common and basic task in Java programming. Knowing the size of an array is essential so that we can perform certain operations. In this article, we will discuss multiple ways to find the length or size of an array in Java.In this article, we will learn:How to
3 min read
How to Initialize a Vector with a Specific Initial Capacity in Java? In Java, Vector Class allows to creation of dynamic arrays that can grow or shrink as per the need. If we know the approximate size of elements, we will store this in the vector. Then we optimize memory usage, and we can initialize it with the specific initial capacity. In this article, we will lear
2 min read