r/programming • • Jun 23 '22

3 Uses of the Three Dots in JavaScript

https://medium.com/techtofreedom/3-uses-of-the-three-dots-in-javascript-8d6b83f179ce
0 Upvotes

3 comments sorted by

13

u/elteide Jun 23 '22 edited Jun 23 '22

const newArray = [...oldArray, item, ...anotherArray];

const [firstItem, ...restArray] = someArray;

const newObject = {...oldObject, someExistingField: "rewritten", newField: "whatever", ...anotherObject}

function f(...items) { for (const i of items) doStuff(i) }

f([1,2,3]);

f(1, 2, 3);

8

u/[deleted] Jun 23 '22

When you don’t know what the operator is called but you are itching to write a tech blog post this week

(yes, they say it later on, but still funny imo)

3

u/Ameisen Jun 24 '22

Looks at picture

There are four lights dots!