JavaScript Array fill() Method

You are Here:

JavaScript Array fill() Method

The fill() method fills (modifies) all the elements of an array from a start index to an end index with a static value.

Note: This method overwrites the original array.

Fill All Elements in an Array

In the following example, we will omit the start index and end value to fill all the elements in an array.

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <script> var myNumber = [1, 2, 3, 4, 5, 6, 7]; var result = []; //fill with * result = myNumber.fill("*"); document.write(result); </script> </body> </html>

Syntax

Array.fill(value, start, end)

Parameter Values

ValueTypeExplanation
valueRequiredSpecifies a value to fill an array.
startOptionalSpecifies the starting position.
If start is omitted, the default value is 0.
endOptionalSpecifies the ending position.
If end is omitted, the default value is the length of an array.

Fill Specific Elements in an Array

In the following example, we will specify the start index and end value to fill specific elements in an array.

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <script> var myNumber = [1, 2, 3, 4, 5, 6, 7]; var result = []; //fill with * result = myNumber.fill("*", 3, 5); document.write(result); </script> </body> </html>

Reminder

Hi Developers, we almost covered 97% of JavaScript Tutorials with examples for quick and easy learning.

We are working to cover every Single Concept in JavaScript.

Please do google search for:

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author