Default Parameters
What will be logged? function demo(a, b = () => console.log(a)) { a = 5; b(); } demo(3);
The code will log `5`. When `demo(3)` is called, the parameter `a` is initialized with the value 3. The parameter `b` gets its default value, which is an arrow function referencing `a`. Inside the function body, `a` is reassigned to 5 before `b()` is called. When `b()` executes, it accesses the current value of `a`, which is 5. This example demonstrates two important concepts: 1) Default parameter expressions can reference other parameters, and 2) Arrow functions capture variables by reference, not by value. The arrow function doesn't capture the initial value of `a` (3), but rather maintains a reference to the variable `a`. When the value of `a` changes before the function is called, the arrow function sees the updated value. This behavior can be both powerful and potentially confusing, so it's important to understand how closures and variable references work when using functions as default parameters.