JavaScript: Array.prototype.fill

By Xah Lee. Date: . Last updated: .

New in JS2015.

arrayX.fill(value)
Replace all elements by value. Return the modified arrayX.
arrayX.fill(value, start_index)
Replace start from start_index
arrayX.fill(value, start_index, end_index)
Replace up to end_index, not including it.
const ar = [0,1,2];
console.log( ar.fill (7) ); // [ 7, 7, 7 ]
console.log( ar ); // [ 7, 7, 7 ] 

With 2 arguments:

console.log( [0,1,2,3].fill(9, 0)); // [ 9, 9, 9, 9 ]
console.log( [0,1,2,3].fill(9, 1)); // [ 0, 9, 9, 9 ]
console.log( [0,1,2,3].fill(9, 2)); // [ 0, 1, 9, 9 ]
console.log( [0,1,2,3].fill(9, 3)); // [ 0, 1, 2, 9 ]
console.log( [0,1,2,3].fill(9, 4)); // [ 0, 1, 2, 3 ]

With 3 arguments:

console.log( [0,1,2,3].fill(9,0,0)); // [ 0, 1, 2, 3 ]
console.log( [0,1,2,3].fill(9,0,1)); // [ 9, 1, 2, 3 ]
console.log( [0,1,2,3].fill(9,0,2)); // [ 9, 9, 2, 3 ]
console.log( [0,1,2,3].fill(9,0,3)); // [ 9, 9, 9, 3 ]
console.log( [0,1,2,3].fill(9,0,4)); // [ 9, 9, 9, 9 ]
JS in Depth
XAH  BUY NOW

JS in Depth

JS Obj Ref

DOM


JS Obj Ref

Array

prototype