JS: “arguments” object in function keyword
What is the argument object
The arguments is a builtin variable, available in function body of functions defined via the keyword function.
It is not available in Arrow Function.
The arguments object is a Array-Like Object.
Each index is the value of arguments of a function call.
function ff() { return arguments; } // show the arguments object console.log(ff("cat", "dog")); // [Arguments] { "0": "cat", "1": "dog" } // show its length console.log(ff("cat", "dog").length); // 2 // show all its keys console.log(Reflect.ownKeys(ff("cat", "dog"))); // [ "0", "1", "length", "callee", Symbol(Symbol.iterator) ] // not a true array console.log(Array.isArray(ff()) === false); // true
Purpose of the argument object
In 1995, the purpose of the argument object was to allow function to take arbitrary number of arguments, such as a function for sum.
It is no longer needed today because Rest Parameters feature was added to JavaScript.
🟢 tip: never use the argument object