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
Java Program to Change a Collection to an Array An array is a data structure that can hold a fixed-size, homogeneous collection of elements of the same data type, which can be either primitive data types (e.g., int, float) or object references. However, the size of the array cannot be changed once it is created. On the other hand, a collection is
3 min read
Java Program to Print the kth Element in the Array We need to print the element at the kth position in the given array. So we start the program by taking input from the user about the size of an array and then all the elements of that array. Now by entering the position k at which you want to print the element from the array, the program will print
2 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