JavaScript

June 3, 2023

How to use JavaScript 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

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

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

  1. Declare Four variables d, e, f and g
  2. 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]
  3. The spread(...) operator converts multiple arrays into a single array.
  4. The value of g variable is displayed with the help of console.log inbuilt function

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

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

by : Suhel Akhtar

Quick Summary:

JavaScript Spread (…)Operator works to merge arrays