# fill method

The `fill` method populates an array with the given values. The desired value is specified in the first parameter of the method. In the second and third optional parameters, we set the start and end positions for filling, respectively.

## Syntax

`array.fill(element, [start position, [end position]]);`

## Example

Let's populate an array using this method:

`let res = [1, 2, 3, 4].fill('!'); console.log(res);`

The code execution result:

`['!', '!', '!', '!']`

## Example

Now let's specify from what position we need to fill an array:

`let res = ['a', 'b', 'c'].fill('!', 1); console.log(res);`

The code execution result:

`['a', '!', '!']`

## Example

Let's fill an array by specifying, in addition to the initial, also the final position:

`let res = ['a', 'b', 'c', 'e'].fill('!', 2, 3); console.log(res);`

The code execution result:

`['a', 'b', '!', 'e']`

## Example

And now let's start filling an array from the very end, specifying the index `-1` as the second parameter:

`let res = ['a', 'b', 'c', 'e'].fill('!', -1); console.log(res);`

The code execution result:

`['a', 'b', 'c', '!']`

## See also

- the `unshift` method
  that adds elements to the start of an array
- the `push` method
  that adds elements to the end of an array
- the `shift` and `pop` methods
  that remove elements from an array

← →
↑
en ru