# Omit Positional Function Parameter Similar to Array Destructuring

**URL:** <https://es.discourse.group/t/omit-positional-function-parameter-similar-to-array-destructuring/1363>\
**Category:** 💡 Ideas\
**Tags:** proposal\
**Created:** [June 8, 2022, 4:04am UTC](https://es.discourse.group/t/omit-positional-function-parameter-similar-to-array-destructuring/1363 "2022-06-08T04:04:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![seekr3](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/seekr3/32/476_2.png) [@seekr3](https://es.discourse.group/u/seekr3)\
**Post date:** [June 8, 2022, 4:04am UTC](https://es.discourse.group/t/omit-positional-function-parameter-similar-to-array-destructuring/1363/1 "2022-06-08T04:04:20Z")

</div>

## Reasoning

Currently if you are using a callback function but not using the first, second, etc. argument it's a little awkward. Which currently looks like:

```javascript
object.method((_firstUnusedParam, _secondUnusedParam, thirdParam) => thirdParam)
// or
object.method((_, __, thirdParam) => thirdParam)

```

A couple real world examples

```javascript
// Array.prototype.map
array.map((_value, index) => `item: ${index + 1}`)

// express
app.get('/', (_req, res) => {
  res.send('ok')
})

```

## Idea

Similar to array destructuring

```javascript
const [, , thirdValue] = array

```

It would be convenient if you could omit preceding positional function arguments if you did not need them like this:

```javascript
object.method((, , thirdParam) => thirdParam)

```

Real world examples:

```javascript
array.map((, index) => `item: ${index + 1}`)

// express
app.get('/', (, res) => {
  res.send('ok')
})

```

---

<div class="post-metadata">

**Author:** ![aclaymore](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/aclaymore/32/501_2.png) [@aclaymore](https://es.discourse.group/u/aclaymore)\
**Post date:** [June 8, 2022, 4:23am UTC](https://es.discourse.group/t/omit-positional-function-parameter-similar-to-array-destructuring/1363/2 "2022-06-08T04:23:54Z")

</div>

While this would be fewer characters for the author to type it might not help when reading the code later. The convention of using the single \_ or a prefixed \_ feels easier to follow when quickly glancing through code for me personally.

---

<div class="post-metadata">

**Author:** ![seekr3](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/seekr3/32/476_2.png) [@seekr3](https://es.discourse.group/u/seekr3)\
**Post date:** [June 8, 2022, 4:41am UTC](https://es.discourse.group/t/omit-positional-function-parameter-similar-to-array-destructuring/1363/3 "2022-06-08T04:41:29Z")

</div>

I am not so much worried about the typing, so much as it feels cumbersome declaring variables I'm not going to use. I think

```javascript
(_, _, param) => param

```

would be just as good, but unfortunately you cannot reuse the underscore. But perhaps you are right that just the \_ with the variable name is not so bad.

You don't have the same intuition about the array destructing though, or would you prefer people also do?

```javascript
const [_value1, _value2, value3] = array

```

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [June 8, 2022, 2:29pm UTC](https://es.discourse.group/t/omit-positional-function-parameter-similar-to-array-destructuring/1363/4 "2022-06-08T14:29:58Z")

</div>

This proposal has already been rejected by the committee: [https://github.com/devsnek/proposal-unused-function-parameters](https://github.com/devsnek/proposal-unused-function-parameters)

---

<div class="post-metadata">

**Author:** ![seekr3](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/seekr3/32/476_2.png) [@seekr3](https://es.discourse.group/u/seekr3)\
**Post date:** [June 8, 2022, 5:27pm UTC](https://es.discourse.group/t/omit-positional-function-parameter-similar-to-array-destructuring/1363/5 "2022-06-08T17:27:47Z")

</div>

So it has. Thanks for the info. One of the commit members point out you could co-opt the array syntax for the same purpose.

```javascript
app.get('/', (...[, res]) => {
  res.send('ok')
})

array.map((...[, index]) => `item: ${index + 1}`)

```

Also a little awkward, but maybe useful if you needed to skip two or more parameters, otherwise, for one I'd probably just use the underscore `_`.

```javascript
app.get('/', (_req, _res, next) => {
  console.log('root request')
  next()
})

// vs.

app.get('/', (...[, , next]) => {
  console.log('root request')
  next()
})

```

🤷‍♂️

---

<div class="post-metadata">

**Author:** ![theScottyJam](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/thescottyjam/32/616_2.png) [@theScottyJam](https://es.discourse.group/u/theScottyJam)\
**Post date:** [June 8, 2022, 10:53pm UTC](https://es.discourse.group/t/omit-positional-function-parameter-similar-to-array-destructuring/1363/6 "2022-06-08T22:53:28Z")

</div>

Huh, I kind of liked this idea of omitting function parameters. I can certainly see it being annoying to figure out what to do with optional parameters - Using an underscore works ok for a single parameter, but things get awkward when there's multiple. I've certainly run into this problem a small handful of times.

---

<div class="post-metadata">

**Author:** ![rdking](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/rdking/32/54_2.png) [@rdking](https://es.discourse.group/u/rdking)\
**Post date:** [June 9, 2022, 2:22am UTC](https://es.discourse.group/t/omit-positional-function-parameter-similar-to-array-destructuring/1363/7 "2022-06-09T02:22:46Z")

</div>

Here I come again with another suggestion: What if there were a notation on the function declaration that allowed for optional positioning of those parameter values from the caller? Something like this:

```javascript
function example(param1, ?param2, ?param3, param4) {
   //Do whatever
}

```

This would work out just the same as if the following had been written instead:

```javascript
function example(param1, param2, param3, param4) {
   switch (arguments.length) {
      case 2:
         param4 = param2;
         param2 = void 0;
         break;
      case 3:
         param4 = param3;
         param3 = void 0;
         break;
   }
   //Do whatever
}

```

The general idea is to ensure that all non-optional parameters are filled first, then fill optional parameters in order given additional available parameter data.
