Pass the value of only one argument that you don't want to change the value of the rest argument

Today we will learn how to pass the value of only one argument that you don't want to change the value of the rest argument, or you'd like to pass the value of any argument that we don't care about its position.

In this case, we just pass all of the argument as an object like this:

function xyz(ags) {
  const { x = 1, y = 2, z = 3 } = ags;
  console.log(x, y, z);
}


Usage:

xyz({x:10}) // 10, 2, 3
xyz({z:6}) // 1, 2, 6
xyz({y:8}) // 1, 8, 3
xyz({y:5, x:7, z:6}) // 7, 5, 6


You can use the rest parameter syntax to get all of the rest argument of the function
Syntax: const { x = 1, ...rest } = ags; (rest is an array contain all the rest argument: [y, z])


© 2019 4codev

Created with love by Sil.