JavaScript Interview Question #146
What is optional catch binding?
Generators, Browser Storage, Security & Advanced Runtime Concepts Senior Advanced
Quick Interview Answer
When a catch block does not need the thrown error value, JavaScript allows the catch parameter to be omitted. This is called optional catch binding.
Detailed Explanation
When a catch block does not need the thrown error value, JavaScript allows the catch parameter to be omitted. This is called optional catch binding.
Use it only when the error details are genuinely irrelevant. In production systems, ignoring useful error context can make troubleshooting more difficult, so many catch blocks should still capture and appropriately log or propagate the error.
Code Example
function tryParse(json) {
try {
return JSON.parse(json);
} catch {
return null;
}
}
console.log(tryParse('{"id":1}'));
console.log(tryParse("invalid"));