JavaScript Array fill() Method

Beginner
⏱️ 8 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
In-place set

What You’ll Learn

The fill() method overwrites array elements with a single value, optionally within a start/end range. This tutorial covers syntax, five examples, the exclusive end index, initialization patterns, and how it differs from map() and copyWithin().

01

Syntax

fill(v,s,e)

02

Static value

Same everywhere

03

Range

start / end

04

In-place

Mutates array

05

Init

new Array(n)

06

ES2015

Modern support

Introduction

Need to reset a buffer to zero, mark a slice as inactive, or bootstrap a fixed-length array with defaults? The fill() method sets one value across some or all indexes in a single call.

Unlike map(), which computes a different value per index, fill() always writes the same value you pass in. Unlike copyWithin(), it does not copy from another part of the array—it injects the value you provide.

Understanding the fill() Method

array.fill(value, start, end) writes value into every index from start up to but not including end. When start and end are omitted, the entire array is filled.

The method returns the modified array (same reference), so you can chain further array operations.

💡
Beginner Tip

end is exclusive: arr.fill(10, 1, 4) changes indexes 1, 2, and 3 only. Index 4 keeps its old value.

📝 Syntax

General form of Array.prototype.fill:

JavaScript
array.fill(value, start, end)

Parameters

  • value — value to write into each selected slot.
  • start — optional; first index to fill (default 0).
  • end — optional; stop before this index (default array.length).

Return value

  • The modified array (same reference).
  • Length is unchanged.

Common patterns

  • arr.fill(0) — zero out the whole array.
  • arr.fill(10, 1, 4) — fill indexes 1–3 with 10.
  • new Array(5).fill(false) — create a five-slot boolean array.
  • [...arr].fill(x) — fill a clone, keep the original.

⚡ Quick Reference

GoalCode
Fill entire arrayarr.fill(value)
Fill a rangearr.fill(value, start, end)
Initialize length nnew Array(n).fill(0)
end indexExclusive
Mutates array?Yes

📋 fill() vs map() vs copyWithin() vs Array.from()

Choose based on whether you need one static value, per-index computation, in-array copying, or independent object instances.

fill
same value

Static overwrite

map
per index

Computed values

copyWithin
copy slice

Existing data

Array.from
factory fn

Unique objects

Examples Gallery

Open DevTools Console (F12) or use Try-it labs. Example N maps to ?tryit=N.

📚 Getting Started

Overwrite a portion or the full array.

Example 1 — Fill a Range with One Value

Replace indexes 1 through 3 with 10; index 4 is untouched.

JavaScript
const numbers = [1, 2, 3, 4, 5];

numbers.fill(10, 1, 4);

console.log(numbers);
// [1, 10, 10, 10, 5]
Try It Yourself

How It Works

fill(10, 1, 4) writes 10 at indexes 1, 2, and 3. Because end = 4 is exclusive, index 4 still holds 5.

Example 2 — Fill the Entire Array

Omit start and end to overwrite every slot.

JavaScript
const scores = [88, 92, 75, 60];

scores.fill(0);

console.log(scores);
// [0, 0, 0, 0]
Try It Yourself

How It Works

With no range arguments, fill runs from index 0 through length - 1. Useful for clearing buffers or resetting game boards.

📈 Practical Patterns

Initialization, immutability, and partial resets.

Example 3 — Initialize a Fixed-Size Array

Combine new Array(n) with fill() to bootstrap defaults quickly.

JavaScript
const grid = new Array(8).fill(0);

console.log(grid);
// [0, 0, 0, 0, 0, 0, 0, 0]
console.log(grid.length);
// 8
Try It Yourself

How It Works

new Array(8) creates eight empty slots; fill(0) assigns zero to each. For objects, prefer Array.from({ length: n }, () => ({})) so each slot is independent.

Example 4 — Fill a Clone to Preserve the Original

Spread into a new array before calling fill() when immutability matters.

JavaScript
const original = [1, 2, 3, 4, 5];
const updated = [...original].fill(10, 1, 4);

console.log(original);
// [1, 2, 3, 4, 5]
console.log(updated);
// [1, 10, 10, 10, 5]
Try It Yourself

How It Works

fill() always mutates its receiver. Cloning first is the standard React/Redux-friendly pattern when you need a new array reference.

Example 5 — Reset a Slice of Boolean Flags

Start with defaults, then flip a middle segment to true.

JavaScript
const flags = new Array(8).fill(false);

flags.fill(true, 2, 5);

console.log(flags);
// [false, false, true, true, true, false, false, false]
Try It Yourself

How It Works

fill(true, 2, 5) sets indexes 2, 3, and 4 to true. Indexes 0, 1, 5, 6, and 7 stay false.

🚀 Common Use Cases

  • Buffer clearing — reset numeric buffers to zero before reuse.
  • Grid initialization — create game boards or matrices with defaults.
  • Feature toggles — bulk-set boolean flags in a segment.
  • Placeholder data — pre-fill arrays with null or "" for forms.
  • Typed arrays — same API on Uint8Array and friends.
  • Performance — faster than manual loops for large static fills.

🧠 How fill() Runs

1

Resolve range

Normalize start and end (including negatives) against array length.

Bounds
2

Walk indexes

Loop from start while index < end.

Iterate
3

Assign value

Write the same value reference into each slot.

Overwrite
4

Return array

Same reference, same length, updated contents.

📝 Notes

  • end is exclusive, matching slice and copyWithin.
  • Negative start / end count from the end of the array.
  • Object and array values are shared by reference across filled slots.
  • Avoid new Array(n).fill({})—use Array.from with a factory for unique objects.
  • fill on a sparse array creates defined slots in the filled range.
  • Works on typed arrays with the same signature.

Browser & Runtime Support

Array.prototype.fill() was added in ES2015 (ES6). It is available in all evergreen browsers and modern Node.js versions.

Baseline · ES2015

Array.prototype.fill()

Supported in Chrome 45+, Firefox 31+, Safari 7.1+, Edge 12+, and Node 4+. Not available in Internet Explorer.

97% Modern browser support
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
Array.fill() Excellent

Bottom line: Safe for modern web apps. For IE11, use a simple for-loop or polyfill to assign values.

Conclusion

The fill() method is the fastest way to stamp one value across an array or a slice of it. Remember the exclusive end index, clone when you need immutability, and use Array.from when each slot needs its own object instance.

For computed per-index values, reach for map() instead. For moving existing data inside an array, use copyWithin().

💡 Best Practices

✅ Do

  • Remember end is exclusive
  • Clone with [...arr] before fill when immutability matters
  • Use new Array(n).fill(0) for numeric initialization
  • Use Array.from for arrays of unique objects
  • Chain after fill since it returns the array

❌ Don’t

  • Use fill when each index needs a different computed value
  • Assume new Array(n).fill({}) creates separate objects
  • Forget fill mutates the original array
  • Include the end index in your mental model—it is not filled
  • Rely on it in IE11 without a fallback

Key Takeaways

Knowledge Unlocked

Five things to remember about Array.fill()

Your foundation for static array initialization and resets.

5
Core concepts
🔄 02

In-place

Mutates array.

Mutable
03

end exclusive

Like slice.

Range
🗃 04

Same value

Not per-index.

Static
05

Object refs

Shared slots.

Pitfall

❓ Frequently Asked Questions

fill() sets some or all elements of an array to a single static value. It modifies the array in place and returns the same array reference.
Yes. fill() overwrites existing slots in the array you call it on. Clone first with spread or slice if you need to preserve the original.
end is exclusive, like slice and copyWithin. fill(value, 1, 4) writes at indexes 1, 2, and 3—not index 4.
fill() always works on an existing array. A common pattern is new Array(length).fill(value) to initialize a fixed-size array, or [...arr].fill(value) to fill a copy.
fill() stores the same object reference in every slot. Mutating one element mutates all of them. Use Array.from({ length: n }, () => ({})) for independent objects.
fill() is an ES2015 (ES6) feature. All modern browsers support it; Internet Explorer does not.
Did you know?

Array(3).fill(7) and [7, 7, 7] look similar, but the first builds the array in two steps—create empty slots, then assign. That is why fill pairs so naturally with new Array(length) for pre-sized buffers.

Continue to filter()

Learn how to build a new array containing only elements that pass your test.

filter() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful