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
Fundamentals
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.
Concept
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.
Foundation
📝 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.
Cheat Sheet
⚡ Quick Reference
Goal
Code
Fill entire array
arr.fill(value)
Fill a range
arr.fill(value, start, end)
Initialize length n
new Array(n).fill(0)
end index
Exclusive
Mutates array?
Yes
Compare
📋 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
Hands-On
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.
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.
fill(true, 2, 5) sets indexes 2, 3, and 4 to true. Indexes 0, 1, 5, 6, and 7 stay false.
Applications
🚀 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.
Important
📝 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.
Compatibility
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 ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
BunSupported · JavaScript runtime
Supported
DenoSupported · JavaScript runtime
Supported
Node.jsSupported · Server runtime
Supported
Android WebViewSupported · 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.
Wrap Up
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().
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
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about Array.fill()
Your foundation for static array initialization and resets.
5
Core concepts
📝01
Syntax
(value, start?, end?)
API
🔄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.