JavaScript Program to Create an Array with a Specific Length and Pre-filled Values Last Updated : 16 Jul, 2024 Comments Improve Suggest changes Like Article Like Report 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.Table of ContentMethod 1: Using the Array() Constructor and fill() MethodMethod 2: Using a Loop to Initialize ValuesMethod 3: Using the map() MethodUsing Array.from with a Mapping FunctionMethod 5: Using the Spread OperatorMethod 1: Using the Array() Constructor and fill() MethodThe 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); Output[ 5, 5, 5, 5, 5 ] Method 2: Using a Loop to Initialize ValuesWe 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); Output[ 1, 2, 3, 4, 5 ] Method 3: Using the map() MethodThe 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); Output[ 5, 5, 5, 5, 5 ] Using Array.from with a Mapping FunctionUsing 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); Output[ 0, 0, 0, 0, 0 ] Method 5: Using the Spread OperatorIn 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); Output[ 0, 0, 0, 0, 0 ] Comment More infoAdvertise with us Next Article JavaScript Program to Create an Array with a Specific Length and Pre-filled Values A anjugaeu01 Follow Improve Article Tags : JavaScript Web Technologies Geeks Premier League javascript-array JavaScript-DSA JavaScript-Program Geeks Premier League 2023 +3 More 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 Java Program to Print the Elements of an Array Present on Even Position The task is to print all the elements that are present in even position. Consider an example, we have an array of length 6, and we need to display all the elements that are present in 2,4 and 6 positions i.e; at indices 1, 3, 5. Example: Input: [1,2,3,4,5,6] Output: 2 4 6 Input: [1,2] Output: 2 Appr 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 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 How to Create a HashSet With a Predefined Capacity in Java? In Java, we can create HashSet with a predefined capacity by creating a constructor and passing the capacity as a parameter to it. We have to initialize the capacity using a variable otherwise you can pass a direct value to it. Syntax:HashSet <datatype>myset=new HashSet<>(capacity);Here, 1 min read Like