EN
JavaScript - how to check number of arguments of function?
14 points
Using JavaScript it is possible to check number of arguments of any function in following ways:
- currently called function provides invisible
arguments
variable available only inside function body - we can use it to check passed arguments, - using spread syntax (
...
syntax) we are able to present arguments as array - useful with arrow functions, - using
length
property with function reference we are able to check number of arguments - it doesn't mean the amount of argumkents should be same during calling.
Check below code to see details.
Each function in JavaScript can be called with different number of arguments than are in definition.
xxxxxxxxxx
1
function doSomething(id, action, message) {
2
console.log('Number of arguments for called function is ' + arguments.length);
3
}
4
5
doSomething(10, 'remove', 'Files from recycle bin removing.',
6
'File name 1', 'File name 2', 'File name...');
Spread syntax was used to present arguments as array.
xxxxxxxxxx
1
let doSomething = (args) => {
2
// arguments keyword is not permitted in arrow function
3
4
console.log('Number of arguments for called function is ' + args.length);
5
}
6
7
doSomething(10, 'remove', 'Files from recycle bin removing.',
8
'File name 1', 'File name 2', 'File name...');
This approach can be used for classic and arrow function definitions.
xxxxxxxxxx
1
function doSomething(id, action, message) {
2
// function logic...
3
}
4
5
console.log('Number of arguments for function definition is ' + doSomething.length);