How to use JavaScript Spread (…)Operator

Spread Operator

Spread (…)Operator

Program to use JavaScript Spread (…)Operator

let a = [1,2,3,4,5];
    let b = [6,7,8];
    let c = [...a,...b]
    console.log(c);
    

Output

[
      1, 2, 3, 4,
      5, 6, 7, 8
    ]
    

Steps Description

  • Declare three variables a, b and c
  • Initialize three variables with array value respectively a = [1,2,3,4,5], b = [6,7,8] and c = […a,…b]
  • The spread(…) operator converts multiple arrays into a single array.
  • The value of c variable is displayed with the help of console.log inbuilt function

Spread (…)Operator

Program to use JavaScript Spread (…)Operator

let d = [1,2,3,4,5];
    let e = [6,7,8];
    let f = [9,10];
    let g = [...d,...e,...f];
    console.log(g);
    

Output

[
      1, 2, 3, 4,  5,
      6, 7, 8, 9, 10
    ]
    

Steps Description

  • Declare Four variables d, e, f and g
  • Initialize Four variables with array value respectively d = [1,2,3,4,5], e = [6,7,8], f = [9,10] and g = […d,…e,…f]
  • The spread(…) operator converts multiple arrays into a single array.
  • The value of g variable is displayed with the help of console.log inbuilt function

Spread (…)Operator

Program to use JavaScript Spread (…)Operator

let h = ['Apple', 'Mango', 'Banana'];
  let i = ['Orange', 'Pomegranate'];
  let j = [...h,...i];
  
  console.log(j);
  

Output

[ 'Apple', 'Mango', 'Banana', 'Orange', 'Pomegranate' ]
  

Steps Description

  • Declare three variables h, i and j
  • Initialize three variables with array value respectively h = [‘Apple’, ‘Mango’, ‘Banana’], i = [‘Orange’, ‘Pomegranate’] and j = […h,…i]
  • The spread(…) operator converts multiple arrays into a single array.
  • The value of j variable is displayed with the help of console.log inbuilt function