In this example we pass 3 arguments: type, color and size to the createShapeWithParams function.
This is completely fine, but what if we’d like to pass 5 or even 10 arguments? In such case it might be better and easier to to pass config object as a single argument.

function createShapeWithParams(type, color, size) { 
 console.log(`Type: ${type}`);
 console.log(`Color: ${color}`);
 console.log(`Size: ${size}`);
}


createShapeWithParams('triangle', 'green', 20);

//Outputs
//Type: triangle
//Color: green
//Size: 20

This is useful because:

  • you don’t have to remember order of arguments
  • it’s safe when skipping optional arguments
  • it’s easier to add or remove arguments

Downsides:

  • you need to remember names of arguments,
  • object properties couldn’t be changed minified
function createShape(cfg) {
 const {
 type,
 color,
 size,
 } = cfg
 
 console.log(`Type: ${type}`);
 console.log(`Color: ${color}`);
 console.log(`Size: ${size}`);
}

createShape({
 type: 'square',
 size: 40,
 color: 'red',
})

//Outputs
//Type: square
//Color: red
//Size: 40