Lodash _.fill() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Array

Definition

Fills elements of array with value from start up to, but not including, end. Part of Lodash Array since 3.2.0 (stable in 4.x — not deprecated). Warning: this method mutates array.

Pass the required arguments shown below. Lodash Array helpers usually return a new value — check whether the source array is mutated.

Syntax

Call _.fill on an array. Start with this line.

Lodash
_.fill(array, value, [start=0], [end=array.length])
CallWhat it does
_.fill(array, value, [start=0], [end=array.length])Primary signature.
_.fill(array)Call with an array argument.

Prefer import fill from "lodash/fill" when you want a smaller bundle.

Library declarations
Lodash
_.fill(array, value, [start=0], [end=array.length])

Parameters

ParameterDescription
arrayRequired. Target array (mutated)
valueRequired. Value written into each slot
startOptional. First index to fill; default 0
endOptional. Exclusive end; default array.length

Return Value

Return ValueDescription
*The same array reference

Lodash Examples

Example 1 — Official: Fill the Whole Array

Omit the range to overwrite every index.

Lodash
var array = [1, 2, 3];

_.fill(array, "a");
// array → ["a", "a", "a"]
Try It Yourself

How It Works

  1. Source. Three numbers in array.
  2. Defaults. start = 0, end = 3.
  3. Write. Every index becomes "a".
  4. Mutate. The original variable now holds the filled array.

Example 2 — Official: Fill Array(3)

Create length-only arrays, then fill the empty slots.

Lodash
_.fill(Array(3), 2);
// → [2, 2, 2]
Try It Yourself

How It Works

  1. Sparse start. Array(3) has length 3 and empty holes.
  2. Fill. Lodash writes 2 into indexes 0 … 2.
  3. Dense result. You get a normal dense array of three 2s.
  4. Use. Handy for placeholders, scores, or default buffers.

Example 3 — Official: Start & End Range

Limit the write to a half-open interval [start, end).

Lodash
_.fill([4, 6, 8, 10], "*", 1, 3);
// → [4, "*", "*", 10]
Try It Yourself

How It Works

  1. Range. Start at index 1; stop before index 3.
  2. Touched. Only indexes 1 and 2 become "*".
  3. Untouched. Indexes 0 and 3 keep 4 and 10.
  4. Rule. end is exclusive — same mental model as slice.

Example 4 — Same Reference Returned

The return value is the mutated array — not a copy.

Lodash
var nums = [1, 2, 3];
var same = _.fill(nums, 0);

console.log(nums);          // [0, 0, 0]
console.log(same === nums); // true
Try It Yourself

How It Works

  1. Call. _.fill writes into nums.
  2. Return. The function hands back the same array object.
  3. Identity. same === nums is true.
  4. Tip. Clone with _.slice(nums) first if you need the old values.

Example 5 — Shared Object Reference

Filling with one object puts the same instance in every slot.

Lodash
var rows = _.fill(Array(2), { n: 1 });

rows[0].n = 99;
console.log(rows[1].n); // 99  (same object)
Try It Yourself

How It Works

  1. One object. { n: 1 } is created once.
  2. Fill. Both indexes store that same reference.
  3. Mutate. Changing rows[0].n also changes rows[1].n.
  4. Fix. Map new objects: Array.from({ length: 2 }, () => ({ n: 1 })).

Browser & Runtime Support

_.fill is a Lodash 4.x Array helper (since 3.2.0). It runs anywhere Lodash runs — modern browsers (via CDN or bundle) and Node.js / Bun / Deno with npm install lodash. Native Array.prototype.fill is also available in modern JS engines.

✓ Stable · Lodash 3.2.0+

_.fill()

Available in [email protected] full builds and the lodash/fill modular import. Works in evergreen browsers and current Node LTS when Lodash is installed.

100% With Lodash 4.x
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
_.fill Lodash 3.2.0+

Bottom line: Safe for production once Lodash is on the page or in your bundle. Prefer import fill from "lodash/fill" for smaller builds — or native array.fill() when you do not need Lodash.

Frequently Asked Questions

_.fill(array, value, [start=0], [end=array.length]) writes value into every index from start up to, but not including, end. It mutates the array and returns the same array reference.
Yes. Lodash changes the existing array in place and returns that same reference. Clone first (for example with slice) if you must keep the original values.
Exclusive — same idea as native Array.prototype.fill and slice. fill(arr, v, 1, 3) updates indexes 1 and 2 only.
Behavior is very similar for arrays. Lodash’s helper is useful for consistent Lodash-style code and older environments where you already ship Lodash. Prefer native fill when you only need modern JS.
Yes, but every filled slot gets the same object reference. Mutating one index’s object changes all indexes that share it — create a new object per slot if you need independence.
Use import fill from "lodash/fill" or require("lodash/fill") so bundlers can tree-shake unused Lodash code. Full build: import _ from "lodash" then _.fill.

Next: _.findIndex()

Locate the first element that matches a predicate.

_.findIndex() 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.

10 people found this page helpful