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
<code class="javascript">function greet() { console.log("Hello!"); }</code>
1 parameters
<code class="javascript">function greet(name) { console.log("Hello, " + name + "!"); }</code>
Multiple parameters
<code class="javascript">function calculateArea(length, width) { return length * width; }</code>
Variable number of parameters (rest parameters)
...
operators to define rest parameters, Indicates that the function can accept any number of parameters, for example: <code class="javascript">function sum(...numbers) { let total = 0; for (const num of numbers) { total += num; } return total; }</code>
Default value parameters
=
operator definition Default value parameter, indicating that the default value is used when the corresponding parameter is not provided, for example: <code class="javascript">function greet(name = "John") { console.log("Hello, " + name + "!"); }</code>
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!