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!
The functions have different structures such as parameters or no parameters and some function return values and some do not in JavaScript. The simplest of function is the one without an parameters and without return. The function compute its statements and then directly output the results.
In JavaScript, function parameters default to undefined . However, it's often useful to set a different default value. This is where default parameters can help.
Optional parameters are great for simplifying code, and hiding advanced but not-often-used functionality. If majority of the time you are calling a function using the same values for some parameters, you should try making those parameters optional to avoid repetition.
You can pass NULL as a function parameter only if the specific parameter is a pointer. The only practical way is with a pointer for a parameter. However, you can also use a void type for parameters, and then check for null, if not check and cast into ordinary or required type.
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");
form_senden("abc"); is ok
the other parameters will be initialized as undefined
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With