Home  >  Article  >  Web Front-end  >  How to set the default value of JS custom function_javascript skills

How to set the default value of JS custom function_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:28:081422browse

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.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn