Filling Arrays with Ease: A Guide to JavaScript’s Array.fill() Method

How do we fill the array in JavaScript? Let’s explore the power of JavaScript’s Array.fill() method with our in-depth guide.

Whether you’re a beginner or an experienced developer, this guide has something for everyone.

In this article, you will learn how to quickly and easily populate arrays with static values, along with examples.

Don’t miss out on this essential resource for mastering the Array.fill() method to enhance your skills in JavaScript.

What is array.fill in JavaScript?

The Array.fill() method in JavaScript is used to fill all the elements of an array with a static value, from a start index (default 0) to an end index (default array.length).

Syntax

The syntax for the Array.fill() method in JS is as follows:

array.fill(value[, start[, end]]) ✅

Parameters

📌value (required)

The value to fill the array with.

📌start (optional)

The index to start filling the array from. If not provided, it defaults to 0.

📌end (optional)

The index to stop filling the array at. If not provided, it defaults to array.length.

Return value

It returns the modified array.

Here’s an example of using the Array.fill() method with all its parameters on an array of subjects:

const subjects = ["English", "Math", "Science", "History"];
subjects.fill("Programming", 2, 4); 
console.log(subjects); 

Output:

["English", "Math", "Programming", "Programming"]

In this example, we create an array of subjects and then use the fill() method to fill the elements from index 2 to index 4 (not inclusive) with the value “Programming.”

The resulting array contains the values [“English”, “Math”, “Programming”, “Programming”].

How to fill arrays in JavaScript? 5 methods

There are several ways to fill an array with values in JS. Here are 5 common methods:

Method 1: Use the Array.fill() method

The Array.fill() method can be used to fill all the elements of an array with a static value.

Here’s an example:

const text = new Array(5); text.fill("Itsourcecode"); 
console.log(text); 

Output:

[
  'Itsourcecode',
  'Itsourcecode',
  'Itsourcecode',
  'Itsourcecode',
  'Itsourcecode'
]

Method 2: Use a loop

You can use a loop to iterate over the elements of an array and assign values to them one by one.

Here’s an example:


var arry = ["how to ", "fill array", " in javascript"];
for (var i = 0; i < arry.length; i++) { 
    arry[i] = 'Hi, welcome to itsourcecode';
}
console.log(arry);

In this example, we create an array of strings and then use a loop to iterate over the elements of the array and assign the value “Hi, welcome to itsourcecode” to each element.

The results of an array contain only “Hi, welcome to itsourcecode” elements.

Output:

[
  'Hi, welcome to itsourcecode',
  'Hi, welcome to itsourcecode',
  'Hi, welcome to itsourcecode'
]

Method 3: Use the Array.from() method

The Array.from() method can be used to create a new array with the specified length and fill it with values generated by a function.

Here’s an example:

const squares = Array.from({length: 5}, (x, i) => (i + 5) ** 2); 
console.log(squares); 

Output:

[ 25, 36, 49, 64, 81 ]

Method 4: Use the spread operator

You can use the spread operator (…) to create a new array and fill it with values from another array or iterable object.

Here’s an example:

const subjects = ["English", "Math", "Science"];
const moresubjects = [...subjects, "Programming", "Web Designing"]; 
console.log(moresubjects);

Output:

[ 'English', 'Math', 'Science', 'Programming', 'Web Designing' ]

Method 5: Use the concat() method

The concat() method can be used to merge two or more arrays and create a new array containing all the elements from the original arrays.

Here’s an example:

const sample1 = [10, 20, 30];
const sample2 = [40, 50];
const allsample = sample1.concat(sample2); 
console.log(allsample);

How to fill empty array in JavaScript?

To fill an empty array in JavaScript you can use the provided method above:

  1. Using a loop
  2. Using the Array.fill() method
  3. Using the Array.from() method

Conclusion

In conclusion, the article discusses various methods to fill arrays in JavaScript.

It highlights the Array.fill() method as one of the options to fill all elements of an array with a static value.

This article also provides examples of other methods, including using loops, the Array.from() methodthe spread operator, and the concat() method.

We are hoping that this article provides you with enough information that help you understand how to fill an array in JavaScript.

You can also check out the following article:

Thank you for reading itsourcecoders 😊.

Frequently Asked Questions

Is JavaScript still worth learning in 2026?
Yes. JavaScript runs on 98% of websites for the front-end, dominates the back-end via Node.js, powers mobile apps through React Native, builds desktop tools through Electron, and is the scripting layer for most AI tooling (LangChain.js, OpenAI SDK, Vercel AI). Whether you target web, mobile, AI, or full-stack capstones, JavaScript is the broadest single language you can learn.
What is the difference between var, let, and const?
var is function-scoped, hoisted to the top of its scope, and can be redeclared, which leads to bugs in modern code. let is block-scoped (only visible inside the nearest {}) and can be reassigned. const is block-scoped and cannot be reassigned, although object contents can still mutate. Default to const for everything, switch to let only when you actually need to reassign, and avoid var in any code written after 2017.
Which JavaScript version should I target in 2026?
Target ES2020 (ES11) as the safe baseline because every modern browser and Node.js 14+ supports it fully. ES2022 adds useful features like top-level await, private class fields with the # prefix, and the .at() array method. If you are writing for older browsers (IE11 or older Android WebViews), transpile down with Babel or use a build tool like Vite, esbuild, or webpack.
What is the best free editor for JavaScript?
Visual Studio Code is the industry standard, free, with built-in IntelliSense, debugger, terminal, Git, and a huge extension marketplace (ESLint, Prettier, GitHub Copilot, Tailwind). Install the JavaScript and TypeScript Nightly extension for the latest language features. JetBrains WebStorm is more powerful and free for students with a verified .edu email. For quick scratchpad work, the Chrome DevTools Sources panel includes a workspace and breakpoint debugger.
How do I run JavaScript locally vs in the browser?
In the browser: open DevTools with F12 (or right-click then Inspect), go to the Console tab, type or paste your code, press Enter. For HTML pages, add a script tag pointing to your .js file. Locally with Node.js: download Node from nodejs.org (LTS version), then run node script.js in your terminal from the file folder. Use the same Node setup for backend capstones, API integrations, and scripts that do not need a browser.
What can I build with JavaScript for my BSIT capstone?
Common BSIT capstones in JavaScript: full-stack web apps using React or Vue on the front-end with Node.js and Express on the back-end (MongoDB or MySQL for the database), real-time chat or notification systems using Socket.io, single-page dashboards with Chart.js or D3.js, cross-platform mobile apps with React Native, AI-powered chatbots using OpenAI SDK and LangChain.js, and Chrome extensions for productivity tools. Add Tailwind CSS for the UI and Vercel or Netlify for free deployment.
Caren Bautista

Technical Writer at PIES IT Solution

Responsible for crafting clear, well-structured, and beginner-friendly content across the platform. Handles the writing, proofreading, and editorial review of tutorials, guides, and documentation to ensure every article is accurate, readable, and easy to follow.

Expertise: Technical Writing · Content Creation · Documentation · Editorial Writing · JavaScript · TypeScript · Python · Python Errors · HTTP Errors · MS Excel  · View all posts by Caren Bautista →

Leave a Comment