Home TutorialsModern ES6 Syntax Spread Syntax

Spread Syntax

by TheScriptKiddie

Spread Syntax

The spread syntax is used to unwind items out of an array and spread them into anywhere a comma separated list of items is used.

The spread operator can be used to unwind the items in an array in order. This is useful to copy elements from one array into another, or unwind elements into a function call.

let values = [15, 16, 23];

let valuesCopy = [...values];

let favoriteNumbers = [4, 8, ...values, 42];

console.log(favoriteNumbers); // [4, 8, 15, 16, 23, 42]

function add(a, b) {
    return a + b;
}

let operands = [5, 7];
add(...operands); // 12

You may also like

Leave a Comment