Matija Matija - 2 months ago 17
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
f
. I need to do that this way:

example(3,1,1,1,5)


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

example(3[1],5[5])


where numbers
[1]
and
[5]
determine the index of argument?

Answer

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});