Learn Before
Code
Array.prototype.fill()
The fill() function alters all the elements in an array by assigning them the same fixed value. By default, it starts from the first index (0) and goes up to the end index (which is the length of the array). After the operation, the function returns the modified array.
let arr = [1, 2, 3, 4, 5]; // fill the entire array with value 0 arr.fill(0); console.log(arr); // [0, 0, 0, 0, 0] // fill a specific portion of the array with value 10 arr.fill(10, 1, 3); console.log(arr); // [0, 10, 10, 0, 0]
0
1
Updated 2023-03-23
Tags
Object-Oriented Programming (OOP) Languages
Object-Oriented Programming
Programming Language Paradigms
General programming languages
Computing Sciences