Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> JavaScript

Có thể có tham số đầu tiên với giá trị mặc định trong JavaScript?

Bạn có thể sử dụng mảng destructured trong trường hợp này.

Ví dụ

function multiply(firstParameterDefaultValue = 10, secondParameterValue) {
  return firstParameterDefaultValue * secondParameterValue;
}
console.log("Kết quả=" + multiply(...[, 10]));

Để chạy chương trình trên, bạn cần sử dụng lệnh sau:

node fileName.js

Tên tệp của tôi là demo173.js.

Kết quả

Chương trình sẽ cho ra kết quả sau:

PS C:\Users\Amit\javascript-code> node demo173.js
Kết quả=100