Using named parameters in ES6

Now with ES6 you can simulate named parameters in JavaScript using the new object destructuring features.

Below is an example of a function with named parameters where all parameters are optional.

function something({ a = true, b = 'something...', c = [] } = {}) {
    // do your magic here...
  }
  
  // Usage:
  sendEmail({ to: 'a@example.com' })
  

You can also combine this with positional arguments like so:

function something(a, b, { c = 1 }) {
    //...
  }
  

← Read all articles