0

argument.length always returns 5 while gives an error in browser

I know that arrow functions does not have arguments then why it is returning 5.

t =()=>{ console.log(arguments.length) } t();

arguments.length should have return error or 0

niz
  • 59
  • 5
  • In arrow functions use rest parameters, eg. `t =(...args)=>{ console.log(args.length) }` In fact in none arrow functions use rest parameters as there just better. – Keith Jul 24 '19 at 11:10

1 Answers1

3

The arrow function returns 5 because of using node,an arrow function inside a regular function

Arrow functions don't really have an arguments object.You can refer this for more information Official information on `arguments` in ES6 Arrow functions?

But why does it always return 5 and not undefined?? The reason behind this is in node your arrow function is wrapped around in a regular function while using the node command. And as regular functions have arguments this argument is being used by your arrow function. When you execute your arrow function it does not find arguments in its execution context and goes a level up to find it and as your function is wrapped by a normal function by node it finds the arguments object and uses it. Although this behavior will only be shown in node and not in browser as it will not have arguments in any execution context.

The reason why 5 is being returned is because the 5 arguments are (exports, require, module, __filename, __dirname) and an easy way of confirming this is make an error in your on the very first line of the JS file.

Vishal Ambre
  • 143
  • 5