Fills given range of array elements with given value.
Syntax:
arr.fill(value[, start[, end]])
Parameters:
- Value:- Value to be filled.
- Start:- Start index (included) and its defaults value is 0.
- End:- End index (excluded) and its default index is this.length.
Return value:
It return the modified array.
Browser Support:
Here in 2nd column int values is the version of the corresponding feature browser.

<script> // input array contain some elements. var array = [ 1, 2, 3, 4 ]; // Here array.fill function fills with 0 // from position 2 till position 3. console.log(array.fill(0, 2, 4)); </script> |
chevron_right
filter_none
Output:
> Array [1, 2, 0, 0]
Program 2:
Let’s see JavaScript program:
<script> // input array contain some elements. var array = [ 1, 2, 3, 4 ]; // Here array.fill function fill with // 9 from position 2 till end. console.log(array.fill(9, 2)); </script> |
chevron_right
filter_none
Output:
> Array [1, 2, 9, 9]
Program 3:
Let’s see JavaScript program:
<script> // input array contain some elements. var array = [ 1, 2, 3, 4 ]; // Here array.fill function fill with // 6 from position 0 till end. console.log(array.fill(6)); </script> |
chevron_right
filter_none
Output:
> Array [6, 6, 6, 6]
Recommended Posts:
- JavaScript Course | Understanding Code Structure in JavaScript
- Introduction to JavaScript Course | Learn how to Build a task tracker using JavaScript
- JavaScript Course | Loops in JavaScript
- JavaScript Course | Data Types in JavaScript
- JavaScript Course | Printing Hello World in JavaScript
- JavaScript Course | Logical Operators in JavaScript
- JavaScript Course | What is JavaScript ?
- JavaScript Course | Operators in JavaScript
- JavaScript Course | Functions in JavaScript
- JavaScript Course | Variables in JavaScript
- JavaScript Course | Conditional Operator in JavaScript
- JavaScript Course | Objects in JavaScript
- JavaScript Course | JavaScript Prompt Example
- JavaScript vs Python : Can Python Overtop JavaScript by 2020?
- How to include a JavaScript file in another JavaScript file ?
- How to compare two JavaScript array objects using jQuery/JavaScript ?
- JavaScript dataView.getInt16() Method
- JavaScript | RegExp toString() Method
- JavaScript | clearTimeout() & clearInterval() Method
- JavaScript Math cosh() Method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


