profileRyan KesPGP keyI build stuffEmailGithubTwitterLast.fmMastodonMatrix

Rest Operator (…) in Object Destructuring

Introduction

Introduced in ES2018 to help with destructuring.

Syntax

Basic

const obj = { foo: 1, bar: 2, baz: 3 }
const { foo, ...rest } = obj

console.log(foo) // 1
console.log(rest) // { bar: 2, baz: 3 }

Named parameters

The rest operator can also be used with named parameters:

function func({ param1, param2, ...rest }) {
  // rest operator
  console.log("All parameters: ", { param1, param2, ...rest }) // spread operator
  return param1 + param2
}