-1

I was learning about arrow function in js where I found one question

console.log((function(x, f = () => x) {
  var x;
  var y = x;
  x = 2;
  return [x, y, f()];
})(1));

It's output is 2,1,1 but it should be 1,1,1.

Can anyone explain it?

14
  • 1
    Because, x = 2; Commented Oct 26, 2021 at 12:55
  • 1
    Can you explain why you expect the first element in the result to be 1? Or what this even has to do with arrow functions at all, since the only difference in the result isn't produced by an arrow function? If the only difference you're seeing from observed vs. expected results is the first value in the response, simplify the code and refactor out anything unrelated to that first value. What do you end up with? Commented Oct 26, 2021 at 12:56
  • @Mr.Polywhirl f = () => x what does it mean? Commented Oct 26, 2021 at 12:56
  • 3
    Are you asking why console.log([2, 1, 1]) prints 2, 1, 1 instead of 1, 1, 1? Commented Oct 26, 2021 at 12:56
  • @David Why the value of y is 1? Commented Oct 26, 2021 at 12:57

2 Answers 2

1

Here is the function re-written to show what is happening.

  1. main takes x = 1
  2. defaultFunction returns the original x = 1
  3. resultFn is a function that gets called inside of main that takes x
  4. y is assigned the x that was passed all the way down (local -scope) 1
  5. x is reassigned to 2
  6. The result is 2, 1, 1

This is an exercise on variable/parameter scope.

const main = (x) => {                      // x = 1
  const defaultFn = () => x;               // x = 1
  const resultFn = (x, f = defaultFn) => { // x = 1
    var x;                                 // x is still 1
    var y = x;                             // y = 1
    x = 2;                                 // x = 2
    return [x, y, f()];                    // x = 2, y = 1, f() -> 1
  };
  return resultFn(x);                      // 2, 1, 1
};

console.log(main(1));

Sign up to request clarification or add additional context in comments.

3 Comments

At least for me this behavior is very confusing. I expected var x; to introduce a new, uninitialized variable that shadows the parameter x. I was surprised that y was set to 1 and not undefined. With this question and your answer I learned that var x; is a redeclaration and let x; causes an error.
@jabaa see: Redeclaring a javascript variable (var specific, ah hoisting)
@pilchard Yes, I got it after some testing and reading but I was surprised. JavaScript can be confusing. It's great that var was replaced with let and const.
0

You set x to 2 thus it got returned as 2. Change x to 1. Fixed Code:

console.log((function(x, f = () => x) {
  var x;
  var y = x;
  x = 2;
  return [x, y, f()];
})(1));

7 Comments

What does it mean f = () => x?
@Deepak_8097 () => x defines an arrow function and f = () => x assigns this function to f.
it assigns it if no parameter is passed.
This is a tricky closure situation.
@MXCDR "it assigns x to f". No, it doesn't. x contains a number. f contains a function.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.