Building Your Learning Module...
Getting things ready for you!
Find videos you like?
Save to resource drawer for future reference!
Think of fill() as a paint bucket tool for arrays! It replaces elements with a static value - you can fill the entire array or just a specific range. Perfect for initializing arrays with default values or resetting portions to a specific state.
fill() modifies the original array (it's mutable)! It doesn't create a new array.arr.fill(value)
Replaces every element
arr.fill(val, start, end)
From start to end index
Returns the modified array
Chainable!
arr.fill(value, start?, end?)If you fill with an object or array, all elements will reference the same object! Changing one changes all. Use Array.from() with a function if you need unique objects.
Fill entire arrays or specific ranges
Real-world scenarios for fill()
const arr = new Array(10).fill(0);arr.fill(null, 2, 5); // Reset indices 2-4const defaults = new Array(5).fill('pending');arr.fill(0).map((_, i) => i * 2);Replace elements with static value quickly
Modifies original array, doesn't create new one
Use start/end parameters to fill specific sections
Objects are referenced, not copied - use with care