Saturday, April 16, 2022

spread (...) operator

Spread operator, Spread syntax

ECMAScript, TypeScript, JavaScript

Spread syntax can be used when all elements from an object or array need to be included in a list of some kind. 

Syntax

For function calls:

myFunction(...iterableObj); // pass all elements of iterableObj as arguments to function myFunction

For array literals:

[...iterableObj, '4', 'five', 6]; // combine two arrays by inserting all elements from iterableObj

For object literals (new in ECMAScript 2018):

let objClone = { ...obj }; // pass all key:value pairs from an object

source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax

No comments:

Post a Comment