Learn how to set default parameter values for JavaScript functions.
If a function in JavaScript is called with missing arguments (less than declared), the missing values are set to undefined
.
Sometimes this is acceptable, but sometimes it is better to assign a default value to the parameter:
ECMAScript 2015 allows default parameter values in the function declaration:
function myFunction (x, y = 2) {
// function code
}
Try it Yourself »
Read more about functions in our JavaScript Function Tutorial.
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!