0

Surprising behavior of function parameters default value in JavaScript

```javascript function native(x, f = () => x) { var x; x = 2; return f(); } function compiled(x, f) { if (f === undefined) f = () => x; var x; x = 2; return f(); } native(1); // returns 1 compiled(1); // returns 2 ``` The first function is a simplified version of a quiz question code. I assumed es6 function parameters default values behave just like a check for undefined, which is also basically what babel outputs. I also assumed function parameters are just like any other local variables defined using ```var```. Which means declaring x multiple times using var in the same function shouldn't make a difference (it gets hoisted to the top of the function and doesn't throw any errors) but surprisingly removing the line ```var x;``` from both functions makes them behave the same. Any idea what's going on?

29th Oct 2019, 1:41 PM
Alireza Asgharizadeh
2 ответов
0
Nope, maybe go back to previous lessons to see the answer?
19th Nov 2019, 5:05 PM
TheSTwin
TheSTwin - avatar
0
What lesson? Did you even read the question?
20th Nov 2019, 9:12 AM
Alireza Asgharizadeh