spread operator js es6 code example

Example 1: array spread operator in javascript

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

Example 2: spread operator javascript

const parts = ['shoulders', 'knees']; 
const lyrics = ['head', ...parts, 'and', 'toes']; 
//  ["head", "shoulders", "knees", "and", "toes"]

Example 3: javascript spread operator

function myFunction(v, w, x, y, z) { }
let args = [0, 1];
myFunction(-1, ...args, 2, ...[3]);

Example 4: spread operator es6

[...fruits]

Example 5: es6 spread

const numbers = [43, 3, 11, 54];
const max = Math.max(...numbers); // Spreads numbers as parameters
// max is 54