Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I need to pass empty parameters to a javascript function?

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!

like image 733
frequent Avatar asked Sep 23 '12 21:09

frequent


People also ask

Can a JavaScript function have no parameters?

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.

Do I always have to add parameters to every function JavaScript?

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.

Are JavaScript function parameters optional?

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.

Can I pass NULL as a parameter in JavaScript?

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.


2 Answers

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"); 
like image 112
Gung Foo Avatar answered Sep 18 '22 15:09

Gung Foo


form_senden("abc"); is ok

the other parameters will be initialized as undefined

like image 43
camino Avatar answered Sep 21 '22 15:09

camino