OutputNot attempted
Optional vs default parameters
function sum(numbers: number[], divisor?: number) { const total = numbers.reduce((a, b) => a + b, 0); return divisor ? total / divisor : total;}function greet(name = 'stranger') { return 'hi ' + name;}console.log(sum([1, 2, 3]), sum([10, 20], 2), sum([10, 20], 0));console.log(greet(), greet(undefined), greet(null as any));Spaces and quote style don’t matter · write a thrown error as TypeError
Ctrl Enter
Show answer & explanation
The expected output comes from actually running the snippet; opening this shows it next to the explanation.
An optional parameter is just undefined when omitted. Checking if (divisor) also treats 0 as missing (often a bug; prefer divisor !== undefined). Defaults apply only for undefined, never for null.