JavaScript

July 13, 2023

How to use JavaScript Array fill()

Program to use JavaScript Array fill() method


let a = ["JavaScript","Python","Php","Css","Html"];
let b = a.fill("Java");
console.log(b);

Output


[ 'Java', 'Java', 'Java', 'Java', 'Java' ]

Steps Description

  1. Declare two variable a and b
  2. Initialize two variable with value of array a = ["JavaScript","Python","Php","Css","Html"] and b = a.fill("Java")
  3. Array fill() method fills the elements of the array with the given value.
  4. The value of b variable is displayed with the help of console.log inbuilt function

Program to use JavaScript Array fill() method


let c = ["JavaScript","Python","Php","Css","Html"];
let d = c.fill("Java",1,3);
console.log(d);

Output


[ 'JavaScript', 'Java', 'Java', 'Css', 'Html' ]

Steps Description

  1. Declare two variable c and d
  2. Initialize two variable with value of array c = ["JavaScript","Python","Php","Css","Html"] and d = c.fill("Java",1,3)
  3. Array fill() method fills the elements of the array with the given value.
  4. The value of d variable is displayed with the help of console.log inbuilt function

Tags:

by : Suhel Akhtar

Quick Summary:

JavaScript Array fill() method