Example 1 — Fill a Range
Overwrite indexes 1…3 with 10 (end exclusive).
const numbers = [1, 2, 3, 4, 5];
numbers.fill(10, 1, 4);
console.log(numbers); // [1, 10, 10, 10, 5] How It Works
- Start with five numbers. Indexes run
0…4. - Set the range.
start=1,end=4means write whilei < 4. - Overwrite three slots. Indexes
1,2,3become10. - Ends stay put. Index
0and4keep their original values.

