Can you have more than one optional argument in JavaScript
Say you have a function dog which takes 3 arguments, one required and 2 optional.
dog(dogsName, [tripsOutOfTheCountry], [numberOfDoctorsVisits])
I know you can check for undefined but what if the person doesn't supply tripsOutOfTheCountry because it is optional and just supplies what they think is numberOfDoctorsVisits.
Example run:
dog("Rusty", 10)
They enter 10 for numberofDoctorsVisits leaving tripsOutOfTheCountry blank because it is optional.
So really the program seems tripsOutOfTheCountry as 10, which is really numberofDoctorsVisits
Is there any way to avoid this. I don't think there is any way around this other than for the user to enter 0 for tripsOutOfTheCountry.
Modifiying the above example to dog("Rusty, 0, 10)
I'm just posing this question because I don't know if it is possible.
P.S. sorry for the corny example
This is the same problem in any language which does not support call-time named arguments, but only passes arguments positionally. If you can make a clear rule by which to identify each argument, you can possibly manually figure out what's what; e.g. if arguments have distinct types like numbers and functions, you can test what is assigned to what and shift things around based on their expected type and order. Many libraries which want to expose a flexible API do this, e.g. just look at the variations this jQuery method allows. This is all on you though, Javascript doesn't help you with this at all.
The usual way to do this instead is to pass objects with explicit names:
dog({ name: 'Rusty', numberOfDoctorsVisits: 10 })