Create an Array With Repeated Values in JavaScript
Let’s say you want to create an array that contains the same value multiple times—like ["apple", "apple", "apple"]
. You might need this for setting up default values, initializing placeholders, or even creating mock data. Fortunately, JavaScript gives us a few expressive ways to do this with minimal code and maximum clarity.
In this tutorial, we'll walk through different techniques to generate arrays with repeated values, all with beginner-friendly explanations and clear examples.
Method 1: Using Array.fill()
The fill()
method is the most straightforward way to create an array with repeated values. You first define the size of the array, and then use fill()
to insert the value across all positions.
const repeatedFruits = new Array(3).fill("apple");
console.log(repeatedFruits);
["apple", "apple", "apple"]
Method 2: Using Array.from()
Array.from()
lets you create an array from scratch and gives you a function to fill each slot. You can use it to insert the same value repeatedly, or even generate dynamic values based on the index.
const repeatedBananas = Array.from({ length: 5 }, () => "banana");
console.log(repeatedBananas);
["banana", "banana", "banana", "banana", "banana"]
Method 3: Using a for
Loop
If you prefer the classic way or need to do additional logic during creation, a for
loop gives you full control. This method is especially useful when the repeated value is derived from some logic or user input.
const items = [];
for (let i = 0; i < 4; i++) {
items.push("cherry");
}
console.log(items);
["cherry", "cherry", "cherry", "cherry"]
Method 4: Repeating Patterns with flatMap()
and Templates
If you want to repeat a certain pattern, like ["apple", "banana"], multiple times, you can use a combination of array methods like flatMap()
or even loops with spreads to get what you need.
const template = ["apple", "banana"];
const pattern = Array.from({ length: 3 }, () => template);
const repeatedPattern = pattern.flat();
console.log(repeatedPattern);
["apple", "banana", "apple", "banana", "apple", "banana"]
Which Method Should You Choose?
If you need a clean and modern one-liner, go with fill()
or Array.from()
. For flexibility or additional logic inside each iteration, use a for
loop. And if you're trying to repeat a complex pattern, combining flat()
with mapping methods works wonders.
Final Thoughts
Creating arrays with repeated values is more than just filling space—it's about structuring data with intention. Whether you're initializing a default configuration, mocking repeated user input, or constructing placeholder elements, JavaScript gives you multiple expressive tools to shape your arrays precisely the way you need.
Understanding these patterns not only sharpens your command over arrays—it opens the door to more advanced patterns and cleaner code in real-world projects. So next time you're tempted to copy-paste "apple" ten times, stop and try one of these elegant approaches instead.
Comments
Loading comments...