Making Every Option Optional

Café Linde’s 180-word menu arrives with no boxes ticked on the quote form. Python’s defaults already cover quote(180). Your JavaScript port takes named options but still expects an object. You’ll let callers omit it and pass options between functions without repeating their names.

Leave the Options Out

The destructuring pattern needs an object to read, and a missing argument is undefined. With no object, quote(180) stops with TypeError: Cannot read properties of undefined (reading 'rate'). So give the whole parameter a default:

Language: JavaScript
function quote(
  words,
  { rate = 0.12, rush = false, minimum = 40 } = {},
) {
  const fee = Math.max(words * rate, minimum);
  const days = 2 + Math.floor(words / 2000);
  if (rush) {
    return [fee * 1.5, Math.max(1, Math.floor(days / 2))];
  }
  return [fee, days];
}

console.log(quote(180));
console.log(quote(6200, { rush: true }));

The café gets its minimum fee, and Bauer Legal keeps its rush quote:

Language: Program Output
[ 40, 2 ]
[ 1116, 2 ]

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.