Home >Web Front-end >JS Tutorial >How to set the default value of JS custom function_javascript skills
If you want to set the default value of a to 5, you cannot write it as:
function my(a=5){
xxx;
}
Simple After checking, there are the following ones that can be used:
function my(a){
alert(a||5);
}
function my(a){
a = typeof(a) == 'undefined' ? 5 : a;
}
function my(a){
if(typeof(a) == 'undefined') {
a = 5;
}
}
Personally, I think the second one is more concise and clearer.