ES6 destructuring object assignment function parameter default value

If you use it, and call the function with no parameters, it works:

function drawES6Chart({size="big", cords = { x: 0, y: 0 }, radius = 25} = {}) {
  console.log(size, cords, radius);
 // do some chart drawing
}

drawES6Chart();

if not, an error is thrown:

TypeError: can’t convert undefined to object

function drawES6Chart({size="big", cords = { x: 0, y: 0 }, radius = 25}) {
  console.log(size, cords, radius);
 // do some chart drawing
}

drawES6Chart();

Leave a Comment