PHP前端开发

在js中定义函数可以使用几个参数

百变鹏仔 3天前 #JavaScript
文章标签 几个
javascript 函数的参数数量取决于具体函数的设计,可能为:1)无参数;2)一个参数;3)多个参数;4)可变数量参数(rest 参数);5)默认值参数。

JavaScript 函数定义中的参数数量

JavaScript 函数可以使用以下几种参数数量:

0 个参数

function greet() {  console.log("Hello!");}

1 个参数

function greet(name) {  console.log("Hello, " + name + "!");}

多个参数

function calculateArea(length, width) {  return length * width;}

可变数量参数 (rest 参数)

function sum(...numbers) {  let total = 0;  for (const num of numbers) {    total += num;  }  return total;}

默认值参数

function greet(name = "John") {  console.log("Hello, " + name + "!");}