Home > Web Front-end > JS Tutorial > 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
Release: 2016-05-16 18:28:08
Original
1467 people have browsed it

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.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template