Horje
What is the use of fill() method in JavaScript Arrays ?

The fill() method in JavaScript is used to fill all the elements of an array with a static value. It modifies the original array and returns the modified array.

Example: Here, the fill(0) method is applied to the myArray, which replaces all elements in the array with the value 0.

Javascript

let myArray = [1, 2, 3, 4, 5];
 
// Fill the entire array with the value 0
myArray.fill(0);
 
console.log(myArray); // Output: [0, 0, 0, 0, 0]

Output

[ 0, 0, 0, 0, 0 ]



Reffered: https://www.geeksforgeeks.org


JavaScript

Related
What is the use of the every() method in JavaScript Arrays ? What is the use of the every() method in JavaScript Arrays ?
How to Convert an Array to a String in JavaScript ? How to Convert an Array to a String in JavaScript ?
What is the use of the Array.of() method in JavaScript ? What is the use of the Array.of() method in JavaScript ?
What is a Sparse Array in JavaScript ? What is a Sparse Array in JavaScript ?
What is the use of reverse() method in JavaScript Arrays ? What is the use of reverse() method in JavaScript Arrays ?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
13