Home >>JavaScript Array Reference >JavaScript Array fill() Method

JavaScript Array fill() Method

JavaScript Array fill() Method

JavaScript array fill() method is a method that used to fills the elements of the given array with the specified static values from a starting index position in the array to an ending index. Because the fill() method modifies the original array and returns undefined, if no element satisfies the condition.

Syntax:
arr.fill(value[, start[, end]])

Parameter Values

Parameter Description
value It Required, the value to be fill the array with
start It is Optional, the index to start filling the array and its defaults value is 0
end It is Optional, the index to stop filling the array and its defaults value is this.length

Browser Support

Method Chrome Edge Firefox Safari Opera
fill() 45.0 12.0 31.0 7.1 32.0
Here is an example of JavaScript Array fill() Method:
<!DOCTYPE html>
<html>
<body>
<button onclick="myFill()">Click me</button>
<p id="fill"></p>
<script>
var fname = ["A", "B", "C", "D"];
document.getElementById("fill").innerHTML = fname;
function myFill() {  
  document.getElementById("fill").innerHTML = fname.fill("E");
}
</script>
</body>
</html>
Output:

Example 2:
<!DOCTYPE html>
<html>
<body>
<button onclick="myFill1()">Click me</button>
<p id="fill1"></p>
<script>
var fnum = ["1", "2", "3", "4"];
document.getElementById("fill1").innerHTML = fnum;
function myFill1() 
{  
  document.getElementById("fill1").innerHTML = fnum.fill("5, 6");
}
</script>
</body>
</html>
Output:


No Sidebar ads