Is this the correct way to check for a missing parameter in a function? Would this work in all browsers? How about IE?
function getName(name){
name = name != null ? name : "default";
return name;
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
you can do:
That says if
nameis undefined or falsy (null,0,"",false,{},[]), set it to"default".js(h|l)int will complain about it, but it works at least as far as back as IE7. It’s not invalid code at all or relying on some undocumented behavior either.