Assuming we all know what arguments are in JS functions
Function.arguments
The arguments object is a local variable available within all non-arrow functions.You can refer to a function's arguments inside that function by using its arguments object.
It has entries for each argument the function was called with, with the first entry's index at 0.
Example:
function exampleFunction(a, b, c) {
console.log(arguments) // Arguments {0: 1, 1: 2}
if (arguments.length !== 3) {
throw new Error(`Invalid amount of arguments. Must be 3 and was ${arguments.length}`);
}
}
exampleFunction(1,2) // Error: Invalid amount of arguments. Must be 3 and was 2
Top comments (0)