The number of parameters of JavaScript function depends on the design of the specific function, which may be: 1) no parameters; 2) one parameter; 3) multiple parameters; 4) variable number of parameters (rest parameters); 5) Default value parameters.
Number of parameters in JavaScript function definition
JavaScript functions can use the following number of parameters:
0 parameters
function greet() { console.log("Hello!"); }
1 parameters
function greet(name) { console.log("Hello, " + name + "!"); }
Multiple parameters
function calculateArea(length, width) { return length * width; }
Variable number of parameters (rest parameters)
...
operators to define rest parameters, Indicates that the function can accept any number of parameters, for example: function sum(...numbers) { let total = 0; for (const num of numbers) { total += num; } return total; }
Default value parameters
=
operator definition Default value parameter, indicating that the default value is used when the corresponding parameter is not provided, for example: function greet(name = "John") { console.log("Hello, " + name + "!"); }
The above is the detailed content of How many parameters can be used to define a function in js. For more information, please follow other related articles on the PHP Chinese website!