Lodash _.fill() method
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
startandendbehave (exclusive end), compared to nativeArray.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
constfreezes the binding, not the contents: an array declared withconstcan still have elements reassigned viafill. - 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
_.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 to0andlengthwhen omitted. - Returns: the mutated
arrayreference.
Fill the entire array
Omit start and end to replace every element. The original binding now points at the new values.
import fill from "lodash/fill";
const nums = [1, 2, 3];
fill(nums, "x");
// nums is now ["x", "x", "x"] Fill a sub-range
Indexes from start up to (but not including) end receive the new value; the rest stay untouched.
import fill from "lodash/fill";
const row = [1, 2, 3, 4];
fill(row, 0, 1, 3);
// row is now [1, 0, 0, 4] Return value is the same array
Assigning the result does not create a copy; compare references with Object.is (or === for arrays).
import fill from "lodash/fill";
const a = [1, 2];
const b = fill(a, 9);
Object.is(a, b); // true 📋 _.fill vs native fill
| API | Why Lodash | Note |
|---|---|---|
_.fill(array, value, start, end) | Consistent helper across environments and array-like inputs | Matches familiar Lodash import style for bundlers |
Array.prototype.fill | Built-in, zero dependency | Prefer when you already have a real array instance and no coercion edge cases |
Pitfalls to avoid
Aliased buffers
If two parts of your app share the same array reference, fill surprises distant callers. Clone before filling when ownership is unclear.
One reference everywhere
Filling with {} does not create distinct objects per index; every slot points at the same object literal.
Frozen arrays
Object.freeze (or sealed elements) will throw or fail when fill tries to assign. Thaw or copy first.
❓ FAQ
Summary
- Purpose:
_.fill(array, value, start, end)mutates the half-open range[start, end)withvalue. - Mutation: the input array changes; the returned reference is the same array.
- Next: Lodash _.findIndex(), _.dropWhile() (previous), or the array methods hub.
_.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.
6 people found this page helpful
