How to use JavaScript Array fill()

Array fill()

Array fill() method

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

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

Array fill() method

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

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