Proposal: Skip arguments passed to function

I ran into a case when using a community built SDK around the Investec API where I need to skip a function argument as the skipped argument was not necessary so I set it to undefined in order to the get to the next argument that the function needs.

For example:

function helloWorld(a, b , c) {
  // does something based on the arguments
}

helloWorld('hello', undefined, 'world')

The above got me thinking about how array destructuring works where I can do this:

const [a,,b] = [1,2,3]

Where I can skip the 1-index element in the array so why can't I do that when passing function arguments like this:

function helloWorld(a, b, c) {

}

helloWorld('hello',, 'world')

The skipped argument would be automatically set to undefined and the syntax falls in line with what is possible with array destructuring.

2 Likes

An optional argument shouldn't precede a required one - the best solution here is probably an options object if there's more than one optional arg at a time.

3 Likes

Hey @shailen-naidoo, out of curiosity do you have a link to the Investec API in question? (I work for Investec and am curious!)

Hey @johnnyreilly it is the library built by Frikkie GitHub - FrikkieSnyman/investec-api: A Node module to interact with Investec's Open API

1 Like

It is not about solutions, it is a proposal. Nobody would talk about this if it didn't work at destructuring.

1 Like

@oleedd I agree, proposals are up in the air and not aimed to be a solution. Array destructuring can be solved in other ways but the features was added due to consensus by the community, I still believe my proposal to be valid in the context of modern JS.

I replied to ljharb, which took it as a problem which needs an existing solution, not as a proposal. I had to quote...
I wanted to propose this at the time you did. But first I opened a question: Why no ability to skip arguments at calling functions?
If you proposed not after reading my question, then it is some wonderful coincidence (even the same day).

1 Like

@oleedd Appreciate your input on this! :)

1 Like

So you proposed after reading my question or not? I was sure that yes then. Because such coincidences seem impossible. Just interesting.

I did not know about your post at all when I posted mine. :)

Wow. Then this improbable coincidence is a sign of higher powers.
So, esteemed delegates, lets discuss this proposal. It is not potentially regrettable unlike array methods.

Options object often comes at a cost, especially for small, hot functions, but still substantial even for medium functions. Object creation and tracking (GC), but also since the args are optional, the object passed will have many shapes. From a performance perspective, this is almost a war crime, and one among many reasons a lot of JS-heavy websites load in seconds not milliseconds