1

This is a short piece of code of JavaScript. And why this arrow function is returning answer 6?

var arguments = [1, 2, 3];
function foo(n) {
  var f = () => arguments[0] + n; 
  return f();
}
console.log(foo(3));

I was expecting answer to be 4.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
  • Why did you tag this with html and css when there is no html or css in your question? – Mike 'Pomax' Kamermans Feb 25 '23 at 16:03
  • 5
    Do you know the special meaning of the `arguments` variable? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments – indyteo Feb 25 '23 at 16:03
  • 2
    Also, if you're writing code modern enough to use arrow functions, don't use the legacy `var`, use either `let` (for reassignable variables) or `consts` (for variables that should not be reassignable and should throw an error if anything tries to reassign them anyway). Both of those obey normal block-scoping rules, `var` _very much_ doesn't. – Mike 'Pomax' Kamermans Feb 25 '23 at 16:05
  • 6
    Both `arguments[0]` and `n` point to `3`. The outer variable `arguments` is shadowed by `foo`'s arguments. Arrow functions don't have their own arguments binding so `f` also uses `foo`'s arguments. – Đinh Carabus Feb 25 '23 at 16:05
  • 1
    Well, as already specified in the comments, the keyword `arguments` inside any functions refers to the arguments array passed to that particular function, read about it [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments). Since you're passing 3 to that function and then adding it with the first argument(3 in this case), the result is 6. It won't access the global `arguments` array variable and refer to the local special arguments variable. An easy fix is to change the name of the global `arguments` array. – Abdulrahman Ali Feb 25 '23 at 16:07
  • you could add `console.log(arguments);` in `foo`, before the function code and have a look. – Nina Scholz Feb 25 '23 at 16:09
  • See also [Are 'Arrow Functions' and 'Functions' equivalent / interchangeable?](https://stackoverflow.com/a/34361380/1563833) – Wyck Feb 25 '23 at 16:26

2 Answers2

2

arguments is a keyword accessible inside functions , it returns an array of parameters passed to a function .

you can check is by console.log(arguments) inside the function .

so, change the variable name to something else at line 1.

0

You really need to be careful all is wrong with your code is that you used an invalid variable name arguments is a JavaScript array like object So it is a JavaScript keyword all you have to do is to change your array variable name here is an example:

var num = [1, 2, 3];
function foo(n) {
  var f = () => num[0] + n; 
  return f();
}
console.log(foo(3));

This outputs 4 now.

PADRO
  • 1
  • 2
    `arguments` is not an invalid variable name. If it was, the code wouldn't work and would produce a syntax error. It is valid variable name but *also* has a special meaning in functions. – VLAZ Feb 25 '23 at 16:57