Hiprup

What is optional catch binding?

Optional catch binding (ES2019) lets you omit the error parameter in catch when you don't need it.

  • Before — you had to write catch (e) even if e was unused.

  • After — just catch { } when the error isn't needed.

Small cleanup: use it when you only care that something failed, not why.

// Before ES2019 — must include error parameter
try {
  JSON.parse(input);
} catch (err) {  // 'err' required even if unused
  return defaultValue;
}

// ES2019+ — error parameter optional
try {
  JSON.parse(input);
} catch {         // No parameter needed!
  return defaultValue;
}

// Practical: feature detection
let supportsOptionalChaining;
try {
  eval('const x = {}; x?.y');
  supportsOptionalChaining = true;
} catch {
  supportsOptionalChaining = false;
}

The catch block without a parameter is cleaner when the error object is not needed. Useful for fallback patterns (try feature, catch fall back to default) and feature detection (try syntax, catch if unsupported).

Small but shows you know modern syntax. Before ES2019: catch(err) was required.

Now: catch {} is valid when you don't need the error. Use for: fallbacks and feature detection.

What is optional catch binding? | Hiprup