Array fill() Method - Javascript Array

Javascript examples for Array:fill

Description

The fill() method fills all the elements in an array with a value.

Parameter Values

Parameter Description
value Required. The value to fill the array with
start Optional. The index to start filling the array (default is 0)
end Optional. The index to stop filling the array (default is array.length)

Return Value:

An Array, the changed array

The following code shows how to Fill all the array elements with a value:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
var fruits = ["a","b","c","d","e"];
document.getElementById("demo").innerHTML = fruits;

function myFunction() {//from w w w. j  a va  2 s.c om
    document.getElementById("demo").innerHTML = fruits.fill("A",2,4);
}
</script>

</body>
</html>

Related Tutorials