Lodash _.fill() method

Beginner
⏱️ 7 min read
📚 Updated: May 2026
🎯 3 Code examples
🚀 3 Try-it labs
⚠️ Mutates in place
Lodash

What you’ll learn

  • How _.fill(array, value, start, end) overwrites a range with one repeated value.
  • That this method mutates the array and returns the same reference.
  • How start and end behave (exclusive end), compared to native Array.prototype.fill.
  • Try each example in the editor (?tryit=1, 2, 3) with Lodash from a CDN.

Prerequisites

You understand mutation versus copying; optional prior read _.dropWhile() on this site.

  • You know that const freezes the binding, not the contents: an array declared with const can still have elements reassigned via fill.
  • You can run snippets in Node or open the Try-it labs in a browser.

Overview

_.fill is the Lodash helper aligned with Array.prototype.fill: it writes the same value into each index of the selected range. Unlike most array helpers covered earlier in this track, it mutates the array you pass in.

In-place

Buffers, ring queues, and typed views can be zeroed or reset without allocating a new backing store.

Range control

start and end let you patch a slice instead of the whole collection.

Chain-friendly

The return value aliases the input array, mirroring native fill for sequential chaining.

Syntax

javascript
_.fill(array, value, [start=0], [end=array.length])
  • array: collection to mutate (array-like values are coerced).
  • value: written into each index inside the resolved range (same reference per slot for objects).
  • start / end: half-open interval [start, end); both default to 0 and length when omitted.
  • Returns: the mutated array reference.
1

Fill the entire array

Omit start and end to replace every element. The original binding now points at the new values.

javascript
import fill from "lodash/fill";

const nums = [1, 2, 3];
fill(nums, "x");
// nums is now ["x", "x", "x"]
Try it Yourself
2

Fill a sub-range

Indexes from start up to (but not including) end receive the new value; the rest stay untouched.

javascript
import fill from "lodash/fill";

const row = [1, 2, 3, 4];
fill(row, 0, 1, 3);
// row is now [1, 0, 0, 4]
Try it Yourself
3

Return value is the same array

Assigning the result does not create a copy; compare references with Object.is (or === for arrays).

javascript
import fill from "lodash/fill";

const a = [1, 2];
const b = fill(a, 9);
Object.is(a, b); // true
Try it Yourself

📋 _.fill vs native fill

APIWhy LodashNote
_.fill(array, value, start, end)Consistent helper across environments and array-like inputsMatches familiar Lodash import style for bundlers
Array.prototype.fillBuilt-in, zero dependencyPrefer when you already have a real array instance and no coercion edge cases

Pitfalls to avoid

Shared

Aliased buffers

If two parts of your app share the same array reference, fill surprises distant callers. Clone before filling when ownership is unclear.

Objects

One reference everywhere

Filling with {} does not create distinct objects per index; every slot points at the same object literal.

Immutable

Frozen arrays

Object.freeze (or sealed elements) will throw or fail when fill tries to assign. Thaw or copy first.

❓ FAQ

Yes. Lodash writes into the existing array and returns that same array reference. Clone first (for example with slice or structuredClone for deep needs) if you must preserve the original.
Like native fill, the end index is exclusive. fill(arr, v, 1, 3) touches indices 1 and 2 only.
Lodash normalizes indexes similar to slice semantics (offsets from the length). When in doubt, log the resolved range in a REPL.
Yes, but every slot receives the same reference. Mutating one element affects every index that points at that object unless you store distinct instances per index yourself.
Use import fill from "lodash/fill" or require("lodash/fill") for tree-shaking friendly bundles.

Summary

Did you know?

_.fill mutates the array and returns the same reference, so you can chain further Lodash calls or assign the result knowing it is identical to the original binding.

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