
...rest, ...spread, reduce(), and Two JavaScript Tricks That'll Mess With Your Head
A fun session. Some genuinely useful patterns and two JavaScript behaviours at the end that look like bugs but are actually the language working exactly as designed. ...rest vs ...spread — Same Syntax, Opposite Jobs These both use ... but they do completely opposite things depending on which side of the assignment they're on. That's why they're easy to mix up. ...rest (Left Hand Side) — gathering things together When you see ... on the left side of an assignment or in a function parameter, it's collecting whatever's left into an array: function logAll(first, ...rest) { console.log(first); console.log(rest); } logAll(1, 2, 3, 4, 5); // first → 1 // rest → [2, 3, 4, 5] Everything that didn't get its own name gets gathered into rest. That's why these are called variadic functions — they can accept any number of arguments. ...spread (Right Hand Side) — spreading things apart When ... appears on the right side, it's doing the opposite — unpacking an existing array into individual elements:
Continue reading on Dev.to Webdev
Opens in a new tab



