Do I need to pass empty parameters to a javascript function?-Collection of common programming errors
Say I have a function like this, which i’m calling throughout a script:
function form_senden( form_name, cnf, confirm_txt, trigger_field, ,do_check, chknfcs, allow, errorMsg ){
// do something
}
On most of my function calls, I’m only passing the first parameter.
Question:
Is it ok in this case to omit passing empty parameters like so:
form_senden("abc");
Or do I need to pass all parameters regardless if they are used like so:
form_senden("abc","","","","","","","","");
Thanks!
-
form_senden(“abc”); is ok
the other parameters will be initialized as undefined
-
It is okay to only pass the first parameter as all other will not be set. If you want to set the 1st and 3rd argument, you will need to make the 2nd null, like so:
form_senden("a",null,"b");
-
Omitting function parameters is okay, the missing parameters will have
undefined
value in the function. -
you may do just
form_senden("abc");
putting default values in the function definition.function form_senden( form_name, cnf , confirm_txt , trigger_field , ,do_check , chknfcs, allow, errorMsg ){ if(typeof(cnf)==='undefined') cnf = ''; if(typeof(confirm_txt)==='undefined') confirm_txt = ''; ...and so on // do something }
Originally posted 2013-11-09 18:44:10.