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

Thiết lập giá trị mặc định undefined cho tham số hàm - JavaScript


Khi truyền giá trị undefined, gán giá trị mặc định.

Ví dụ

Mã thực hiện:

function showValue(value) { 
  if (value===undefined) { 
    value=100000; 
  } else { 
    value=value; 
  } 
  return value; 
} 
console.log("The value="+showValue(300)); 
console.log("The value="+showValue(undefined));

Chạy lệnh:

node fileName.js.

Tên file: demo246.js.

Kết quả

Đầu ra console:

PS C:\Users\Amit\javascript-code> node demo246.js 
The value=300 The value=100000