Configuration Objects Lesson

A configuration object groups setup options into one object. Instead of passing many separate values into a function or class, the program passes one object with named properties. This makes setup code easier to read because each value has a label. It also makes optional settings easier to support, because the function can use defaults when a property is missing.

Code Runner Challenge

Configuration Objects Lesson

View IPYNB Source
%%js

// CODE_RUNNER: Configuration Objects Lesson

function createTimer(config) {
  const label = config.label || "Timer";
  const seconds = config.seconds ?? 30;
  const visible = config.visible ?? true;

  console.log(label + " starts at " + seconds + " seconds");
  console.log("Visible: " + visible);
}

createTimer({ label: "Practice Round", seconds: 45 });
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...
The function reads named settings from one object. If a setting is missing, the function uses a default value instead of failing.