fill()

The fill method in JavaScript arrays populates the elements of a specified array with designated static values. This method alters the original array in place. It returns undefined if no element meets the specified condition.

Syntax

The syntax for the fill method is expressed as follows:

Example

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

Parameter

value - The static value to be filled.

start - This parameter is optional. It denotes the index at which the value begins to populate. By default, this value is set to 0.

end - This parameter is not mandatory. It indicates the index at which the value ceases to be filled. By default, it is set to length-1.

Return

The modified array.

JavaScript Array fill method example

Let's see some examples of fill method.

Example 1

In this section, we will supply solely the value that needs to be entered.

Example

<script>

var arr=["AngularJS","Node.js","JQuery"];

var result=arr.fill("Bootstrap");

document.writeln(arr);

</script>

Output:

Output

Bootstrap,Bootstrap,Bootstrap

Example 2

In this instance, we will additionally specify the initial index.

Example

<script>

var arr=["AngularJS","Node.js","JQuery"];

var result=arr.fill("Bootstrap",1);

document.writeln(arr);

</script>

Output:

Output

AngularJS,Bootstrap,Bootstrap

Example 3

In this instance, we will additionally include both the starting and ending indices.

Example

<script>

var arr=["AngularJS","Node.js","JQuery"];

var result=arr.fill("Bootstrap",0,2);

document.writeln(arr);

</script>

Output:

Output

Bootstrap,Bootstrap,JQuery

Input Required

This code uses input(). Please provide values below: