Arrow Functions
What is the value of `output` after this code runs?
const calculate = (a, b) => a + b;
const output = calculate?.(5, 10);
The value of `output` will be `15`. This code demonstrates two concepts: arrow functions and optional chaining (`?.`). The arrow function `calculate` takes two parameters and returns their sum. The optional chaining operator (`?.`) is used to call a function that might be null or undefined—if the value before the operator is null or undefined, the expression short-circuits and returns undefined without throwing an error. In this case, `calculate` is a valid function, so it's called with arguments 5 and 10, resulting in 15. Optional chaining was introduced in ES2020 and is useful for safely accessing and calling properties or methods that might not exist. This example is somewhat contrived since we can see that `calculate` is defined, but in real-world scenarios, this pattern is valuable when dealing with potentially undefined functions, especially when working with external data or APIs where the structure might be uncertain.