let arr1 = ['A', 'B', 'C'];
let arr2 = ['X', 'Y', 'Z'];
let result = [...arr1, ...arr2];
console.log(result); // ['A', 'B', 'C', 'X', 'Y', 'Z']
// spread elements of the array instead of taking the array as a whole
syntax:
...iterable
where, iterable is source like array, object, string etc
const a=[1,2]
console.log(...a)
// prints 1 2
***spread operator makes a copy of source, not reference.
// ex. - if a & b are 2 arrays
a=[...b]
//changing a won't affect b & vice versa
*Also since it copies source, something like this is also possible:
const a=[5,1]
const b=[2]
const c=[...b,...a]
// c is [2,5,1]
*********************************************
For object just use {} instead of []
const a={ap:4}
const c={...a}
var num = [2, 4, 6, 8, 10];
console.log(...num)
//expected output: 2 4 6 8 10
console.log(88, ...num, 99)
// added extra new elements before and after the spread operator
//expected output: 88 2 4 6 8 10 99
let arr1 = ['one', 'two'];
let arr2 = [...arr1, 'three', 'four', 'five'];
console.log(arr2); // ["one", "two", "three", "four", "five"]
let arr1 = [1, 2, 3]
let func = (a, b, c) => a + b + c
console.log(func(...arr1)) // 6
spread operator uses
const newArray = ['firstItem', ...oldArray];
[...fruits]