Matija Matija - 7 months ago 51
Javascript Question

skip function arguments javascript

For example I have this function

function example(a=1,b=1,c=1,d=1,f=1) {
return (a*b*c*d*f)

So I have simple function with parameter which have default value.
And now when I call the function if I want to multiply a with
. I need to do that this way:


Why I can't simply write first and last argument? Is that possible, something like:


where numbers
determine the index of argument?


I would suggest to pass a map Whenever you are not sure about the number of argument you are gonna pass to a function.

If I were in your place, I would have written it as follows.

function example(params) {
    var DEFAULT = 1
    var a = params.a || DEFAULT;
    var b = params.b || DEFAULT;
    var c = params.c || DEFAULT;
    var d = params.d || DEFAULT;
    var e = params.e || DEFAULT;

    return (a*b*c*d*e);

now you can call it like example({a:1, b:3});