If I have a function
foo()
that I call with no arguments most of the time, but one argument in special cases, is
var arg1 = arguments[0];
if (arg1) {
<special case code>
}
inside the function a completely safe thing to do?
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.
Yes, that’s fine. A reasonable alternative is to name the argument, and not use the
argumentsobject:Note that
if(bar)tests the truthiness ofbar. If you callfoowith any falsy value, such asfoo(0),foo(false),foo(null), etc., the special case code will not execute in the above function (or your original function, for that matter). You can change the test toto make sure that the special case code is executed when the argument is supplied but falsy.