| by Arround The Web | No comments

Introduction to array.fill() Method for Beginners

In JavaScript, the array.fill() method is used to place an element into the array from the user-defined starting to ending index position. It must be called through a specific instance of an array class because it belongs to an array object method. This method overwrites the original array and fills a specified element in an array.

The array.fill() method belongs to ECMAScript6. All the modern browsers such as Chrome, Edge, Safari, etc except Internet Explorer 11 support this method.

This article provides a deep insight into the array.fill() method in JavaScript and serves the following learning outcomes:

  • How array.fill() method works in JavaScript
  • How to use array.fill() method in JavaScript

How array.fill() method works in JavaScript

The working of the array.fill() method is described as follows.

Syntax

The following syntax represents the functionality of array.fill() method in javascript.

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

The array.fill() method is using the following parameters.

  • value represents an element to be filled in an array
  • start denotes the index number from where the arr.fill() method starts filling the value. It is optional with 0 default number.
  • end shows the index position where the arr.fill() method stops filling the value in an array. It is optional with a length-1 default value.

The array.fill() method returns a modified/filled array.

How to use array.fill() method in JavaScript

The array.fill() method overwrites the original array and fills the specified element. Here, we will explain the usage of the array.fill() method with examples.

Example 1: How to overwrite an array using the array.fill() method in javascript

The array.fill() method is used to overwrite/modify the original array. This example shows how to modify an array with the array.fill() method in javascript.

var title_array = [ 't' , 'i ', 't' , 'l' , 'e' ];
console.log(title_array.fill( 'z', 0, 2));

In the above code, we have declared an array object “title_array” with 5 elements. The array.fill() method is applied to the “title_array” to modify the array. The ‘z’ element is modified at the first two positions.

The start index number was set to 0 and the ending index number was set to 2 (which states that the elements will be filled up to index number 1=(2-1)). Therefore, the elements at 0th and 1st index are replaced with the ‘z’. 

Example 2: How to replace the elements of an array using the array.fill() method

The array.fill() method is used to fill the original array. This example shows how to fill an array with the array.fill() method in javascript.

var arr=["Javascript", "Html", "Node.js"];
Var result=arr.fill("css");
console.log(result);

Here in this example, we have declared a variable and used the array.fill() method to fill an array. We pass the new value “css” to fill in the existing array.

The output shows that all the elements of the ‘arr’ have been replaced by the ‘css’ element.

Conclusion

In JavaScript, the Array.fill() method is used to place an array element from start to end index position.  In this complete guide, we described an introduction to the array.fill() method in javascript. This step-by-step procedure explains the array.fill() method, syntax, and its functionality with examples.

Share Button

Source: linuxhint.com

Leave a Reply