JavaScript array fill: Array.prototype.fill, start/end, typed arrays, Array.from

How Array.prototype.fill works in JavaScript: javascript array fill, fill array js, array.fill with value and optional start/end, exclusive end, negative indices, object reference gotcha, new Array(n).fill, typed arrays, Array.from for per-index values, generic array-like fill, and push loop alternative. Snippets include short expected-output notes from Node runs.

Published

Updated

Read time 4 min read

Reviewed byDeepak Prasad

JavaScript array fill: Array.prototype.fill, start/end, typed arrays, Array.from

Array.prototype.fill sets a contiguous range of indices in an existing array (or typed array) to one static value, mutates the array in place, and returns the same reference. The end index is exclusive, and negative start / end count backward from length. When you need a new array or a different value per index, prefer Array.from or map instead of stretching fill past its intent—see also shallow copy patterns when you are cloning versus filling.

Tested on: Node.js v20.18.2. A short note after each snippet describes what you should see in the console.


Quick reference

fill is for one repeated value (or one shared reference) across a range; when each index needs its own value or object, switch to Array.from or map.

Approach Mutates input? Typical use
arr.fill(v, start?, end?) Yes Reset or mask a fixed value in place
new Array(n).fill(v) After creation Fixed-length buffer of v
Array.from({ length }, mapFn) No (new array) Different value (or object) per index

fill(value) — fill the whole array fill range

Replace every index with one value.

javascript
let arr = [1, 2, 3, 4];
arr.fill(0);
console.log(arr);
Output

You should see one line like: [ 0, 0, 0, 0 ].


fill(value, start, end) — partial range (end not included)

Indices from start up to but not including end are filled.

javascript
let arr = [1, 2, 3, 4];
arr.fill(0, 1, 3);
console.log(arr);
Output

You should see one line like: [ 1, 0, 0, 4 ].

Indices 1 and 2 are filled; index 3 is left alone because end is exclusive.

javascript
let arr = [1, 2, 3, 4];
arr.fill(undefined);
console.log(arr);
Output

You should see one line like: [ undefined, undefined, undefined, undefined ].


Negative end (counts from the end)

Here end = -1 means stop before the last element, so the last 5 stays.

javascript
console.log([1, 2, 3, 4, 5].fill(0, 1, -1));
Output

You should see one line like: [ 1, 0, 0, 0, 5 ].


Same reference when value is an object

fill stores the same reference in every index—mutating one slot affects all slots that share that object.

javascript
const o = {};
const a = new Array(3).fill(o);
o.x = 1;
console.log(a[0] === a[1], a);
Output

You should see one line like: true [ { x: 1 }, { x: 1 }, { x: 1 } ].

For distinct objects per slot, use something like Array.from({ length: 3 }, () => ({})) instead of fill({}).


new Array(n).fill(value) — common array fill javascript bootstrap

javascript
console.log(new Array(4).fill(0));
Output

You should see one line like: [ 0, 0, 0, 0 ].

An array with length === 0 has nothing for fill to touch; give it a non-zero length first (as here).


Sparse arrays: fill still writes holes

javascript
const sp = [1, , 3];
sp.fill(9);
console.log(sp);
Output

You should see one line like: [ 9, 9, 9 ].


Typed arrays (Uint8Array, etc.)

Values are coerced to the element type (for example Uint8Array clamps to 0–255).

javascript
let arr = new Uint8Array(4);
arr.fill(255);
console.log(arr);
Output

You should see one line like: Uint8Array(4) [ 255, 255, 255, 255 ].

javascript
let arr = new Uint8Array([2, 2, 3, 43, 5]);
arr.fill(255, 1, 3);
console.log(arr);
Output

You should see one line like: Uint8Array(5) [ 2, 255, 255, 43, 5 ].


push in a loop — new array without fill

Useful when building a list from scratch (see also append to an array in JavaScript):

javascript
function filler(value, len) {
  const array = [];
  for (let i = 0; i < len; i++) {
    array.push(value);
  }
  return array;
}
console.log(filler("wow", 4));
Output

You should see one line like: [ 'wow', 'wow', 'wow', 'wow' ].


Array.from — fill array javascript with a per-index callback

The second argument maps each new index; use () => ({}) when each cell must be its own object.

javascript
let arr = Array.from({ length: 7 }, () => "wow");
console.log(arr);
Output

You should see 6 lines of output, starting with [ and ending with ].


Generic array-like objects

fill is generic: it uses this.length and integer keys. For plain objects:

javascript
const al = { length: 3, 0: "a" };
Array.prototype.fill.call(al, 1);
console.log(al);
Output

You should see one line like: { '0': 1, '1': 1, '2': 1, length: 3 }.

Do not use fill on strings; they are immutable (MDN).


Summary

fill mutates and returns the same reference; end is exclusive like slice. Filling with {} shares one object—use Array.from when each slot needs its own instance.

  • fill mutates in place, returns the same array, and treats end like slice (exclusive).
  • Filling with an object repeats one reference; use Array.from for distinct objects per index.
  • Typed arrays follow the same signature with element-type coercion.
  • Prefer Array.from or map when each index needs its own computed value.

References

MDN references for in-place fill, Array.from, and push.


Frequently Asked Questions

1. Does array fill return a new array?

No. fill mutates the array (or typed array) in place and returns the same reference, which lets you chain other mutating methods if needed.

2. Is the end index inclusive in fill(value, start, end)?

No. end is exclusive, like slice. Indices filled are start <= i < end after each index is resolved (including negative indices relative to length).

3. Why do all elements change when I fill with {} and then mutate one slot?

fill stores the same value in every index. For objects, that is the same reference, not a deep copy. Use Array.from({ length: n }, () => ({})) if you need distinct objects.

4. How do I fill array js style when I only know the length?

Use new Array(n).fill(value) to create n slots then fill them, or Array.from({ length: n }, () => value) if you prefer a functional style without a separate resize step.

5. Does fill work on typed arrays for javascript fill tutorials?

Yes. Typed arrays implement fill with the same signature; values are coerced to the element type (for example Uint8Array clamps to 0–255).

6. fill vs map for building an array from scratch?

fill sets every element to one static value (or the same object reference). map derives each element from its index and optionally prior values; use map or Array.from when values should differ per index.
Olorunfemi Akinlua

Boasting over five years of experience in JavaScript, specializing in technical content writing and UX design. With a keen focus on programming languages, he crafts compelling content and designs …