Is it possible to state a function with different number of parametrs in JavaScript? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Is it possible to state a function with different number of parametrs in JavaScript?

Is there a way to somehow decide the number of parameters in a JavaScript function like we can do for MATLAB functions? Also, is it possible to skip a parameter and insert the value for next parameter?

23rd May 2017, 4:33 PM
Gaurav Garg
Gaurav Garg - avatar
2 Answers
+ 1
If you want to skip an argument, just use "undefined" at its position.
23rd May 2017, 5:21 PM
nouseforname
nouseforname - avatar
+ 1
I am not familiar with MATLAB. if you want to skip a parameter and insert the next parameter value, you should place the skipped parameter at last position when you define a function. best way to deal with this kind of situation is to use object as parameter and use the object property as parameter inside function value. function log(param) { // use any param property here or // simply skip any param object property console.log(param.a); console.log(param.b); } var obj = { b: "some value" } add(obj); console.log(param.a) will log undefined since you didn't pass property a you can also pass an empty object, function will be still executed without any error.
23rd May 2017, 5:56 PM
Apel Mahmod
Apel Mahmod - avatar