JavaScript TypedArray fill() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
ES2015

Definition

typedArray.fill(value, start, end) sets the elements from start up to, but not including, end to the same value. It changes the typed array in place and returns the same typed array, so calls can be chained. The value is converted to the element type first: a Uint8Array wraps it, a Uint8ClampedArray clamps it, and a Float32Array rounds it to single precision.

Remember
new Uint8Array(4).fill(7)        → 7 7 7 7
ta.fill(0, 1, 3)                  → only indexes 1 and 2
ta.fill(5, -2)                    → the last two

Uint8Array.fill(300)         → 44 (wraps)
Uint8ClampedArray.fill(300)  → 255 (clamps)
returns the same typed array

Syntax

Only the value is required. The end index is not included.

JavaScript
typedArray.fill(value)
typedArray.fill(value, start)
typedArray.fill(value, start, end)
CallWhat it does
ta.fill(7)Set every element to 7.
ta.fill(0, 2, 5)Set only the elements at indexes 2, 3, and 4 to 0.
ta.fill(1).fill(9, 2)Chain calls, because fill() returns the same typed array.

Available since ES2015 on all 11 types. A BigInt array needs a BigInt value such as 7n.

Library declarations
JavaScript
TypedArray.prototype.fill(value, start = 0, end = length)

// value is converted ONCE to the element type
// negative start/end count from the end
// returns the SAME typed array (this)

Parameters

ParameterDescription
valueThe value to write. It is converted to the element type, so out-of-range numbers wrap (or clamp for Uint8ClampedArray).
start (optional)Index of the first element to change. Negative counts from the end. Defaults to 0.
end (optional)Index to stop before. Negative counts from the end. Defaults to the length.

Return Value

Return ValueDescription
TypedArrayThe same typed array you called it on (not a copy), now with the new values.

JavaScript Examples

Example 1 — Fill the Whole Array or a Range

A row of five pixels starts at 0. We paint all of them, then repaint a range, and check what fill() returns.

JavaScript
const row = new Uint8Array(5);

row.fill(7);
console.log(row.join(' '));          // 7 7 7 7 7

row.fill(0, 1, 3);
console.log(row.join(' '));          // 7 0 0 7 7

const result = row.fill(9, 4);
console.log(row.join(' '));          // 7 0 0 7 9
console.log(result === row);         // true
Try It Yourself

How It Works

  1. No range means everything. The first call writes 7 into all five slots of the byte array.
  2. The end stays untouched. fill(0, 1, 3) changes indexes 1 and 2 only, so the outer 7s stay.
  3. The same array comes back. The last call changes index 4 and returns the very object row, which the strict comparison confirms.

Example 2 — The Value Is Converted to the Type

A fill value that does not fit the type is converted, not rejected. Each type has its own rule.

JavaScript
console.log(new Uint8Array(2).fill(300).join(' '));          // 44 44
console.log(new Uint8Array(2).fill(-1).join(' '));           // 255 255
console.log(new Int8Array(2).fill(200).join(' '));           // -56 -56
console.log(new Uint8ClampedArray(2).fill(300).join(' '));   // 255 255
console.log(new Uint8ClampedArray(2).fill(-5).join(' '));    // 0 0
console.log(new Uint8ClampedArray(2).fill(1.5).join(' '));   // 2 2
console.log(new Uint8ClampedArray(2).fill(2.5).join(' '));   // 2 2
console.log(new Float32Array(1).fill(0.1).join(' '));        // 0.10000000149011612
Try It Yourself

How It Works

  1. Bytes wrap around. In a Uint8Array 300 becomes 44 and -1 becomes 255, while an Int8Array turns 200 into -56.
  2. Clamped bytes stop at the edges. A Uint8ClampedArray stores 255 for 300 and 0 for -5, and rounds halves to the nearest even number, so 1.5 and 2.5 both give 2.
  3. Floats keep single precision. A Float32Array cannot store 0.1 exactly and prints its nearest single-precision neighbour.

Example 3 — Negative Indexes and Chained Calls

Negative start and end count from the end, and because the same array is returned, calls can follow each other. BigInt arrays need a BigInt value.

JavaScript
const levels = new Int16Array(6).fill(-1, -2);
console.log(levels.join(' '));          // 0 0 0 0 -1 -1

const middle = new Int16Array(6).fill(5, 2, -1);
console.log(middle.join(' '));          // 0 0 5 5 5 0

const chained = new Uint8Array(4).fill(1).fill(9, 2);
console.log(chained.join(' '));         // 1 1 9 9

const ids = new BigInt64Array(2).fill(7n);
console.log(String(ids[1]));            // 7
try { new BigInt64Array(2).fill(7); } catch (e) { console.log(e.name); }   // TypeError
Try It Yourself

How It Works

  1. -2 starts at the second-to-last slot. With six elements, fill(-1, -2) writes -1 into indexes 4 and 5 only.
  2. A negative end stops early. fill(5, 2, -1) covers indexes 2, 3, and 4, and leaves the last slot at 0.
  3. Chain and BigInt. The second fill(9, 2) runs on the array the first one returned. BigInt arrays accept 7n and throw a TypeError for a plain 7.

Browser & Runtime Support

TypedArray.prototype.fill() is part of ES2015 (ES6), together with the other array-like typed array methods.

✓ Baseline · ES2015

TypedArray.prototype.fill()

Supported in every modern browser, plus Node.js, Deno, and Bun.

98% Modern browser support
Google Chrome All modern versions
Full support
Mozilla Firefox All modern versions
Full support
Apple Safari All modern versions
Full support
Microsoft Edge All modern versions
Full support
Node.js All current releases
Supported
Deno All releases
Supported
Internet Explorer IE 10 and 11 have typed arrays, but not this method
Not supported
fill() Excellent

Bottom line: Safe everywhere modern JavaScript runs. Internet Explorer 10 and 11 have typed arrays, but not the fill() method.

Frequently Asked Questions

It sets a range of elements, or all of them, to one value. It changes the typed array in place and returns that same typed array.
The value is converted to the element type, and a byte keeps only 8 bits. 300 minus 256 is 44. A Uint8ClampedArray stores 255 instead.
No. It returns the same object, so ta.fill(0) === ta is true. That is why fill() calls can be chained.
No. A new typed array is already filled with zeros. Use fill() for another value or to reset a range later.
Pass start and end: ta.fill(0, 2, 5) changes indexes 2, 3, and 4. Negative numbers count from the end, so ta.fill(0, -2) clears the last two.

Next: filter()

Keep only the elements that pass a test, in a new typed array of the same type.

filter() 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.

11 people found this page helpful