123

Are there any standard ways of marking a function argument as unused in JavaScript, analogous to starting a method argument with an underscore in Ruby?

2
  • 5
    From what i have seen so far, its still _ that's being used. Commented Aug 25, 2015 at 7:27
  • If you're looking to remove unused variables use jslint Commented Aug 25, 2015 at 7:30

7 Answers 7

89

Just so we have an example to work from, this is fairly common with jQuery's $.each where you're writing code that doesn't need the index, just the value, in the iteration callback and you're using this (which jQuery also sets to the value) for something else:

$.each(objectOrArrayLikeThing, (_, value) => { // Use `value` here }); 

(Yes, $.each passes arguments to the callback backward compared to the JavaScript standard forEach.)

Using _ is the closest I've seen to a standard way to do that, yes, but I've also seen lots of others — giving it a name reflective of its purpose anyway (index), combining those (_index), calling it unused, etc.

If you need to ignore more than one parameter, you can't repeat the same identifier (it's disallowed in strict mode, which should be everyone's default and is the default in modules and class constructs), so you have do things like _0 and _1 or _ and __, etc. I've almost never had to do that, but when I have, I've used a name indicating what the parameter's value would be (_index for the $.each example, for instance).

Sign up to request clarification or add additional context in comments.

4 Comments

you can't do function (_, _, value) though
@HanmingZeng - Indeed not. :-( You have to do things like (_0, _1, value) or (in my case) (unused0, unused1, value). I've also seen (_, __, value) but...yikes. :-) There was a strawman proposal to allow blank parameter names once upon a time but it never went anywhere.
In Chromium it seems to work just fine to write a function declaration like function (_, _, a) { … } (of course inside the function the variable _ only contains the second passed argument) but there's no error and the function is created (and works) just fine.
@zrajm - Only in loose mode. In strict mode, duplicate parameter names are disallowed. (It's best to always use strict mode except in rare situations where you can't. It's also the default for modules and the content of class constructs.)
66

Using destructuring assignment, one can do:

function f(...[, , third]) { console.log(third); } f(1, 2, 3);

1 Comment

I went with this solution since eslint would raise an error when I use _: Variable name _ must match one of the following formats: camelCase, PascalCase, UPPER_CASE
30

With browsers supporting destructuring one can do:

function ({}, {}, value) { // console.log(value) } 

Which is kind of neat in that it avoids the problem of multiple arguments having the same name and also won't create problems with libraries that assign methods to _ (lodash, underscore, etc.).

One problem with this approach is that unused arguments of type undefined or null will throw.

For undefined one solution is to use default parameters:

function ({}={}, {}={}, value) { // console.log(value) } 

Sadly no such easily applied solution for null.

4 Comments

Unfortunately, this throws if an unused argument is undefined or null.
@OlegsJeremejevs function ({}={}, {}={}, value) { ... } helps here, but it becomes messy...
@oluckyman Nice workaround! "Don't want to name an unused arg? Just dumbbell it!"
Wow, I always thought that destructuring a primitive value, i.e. (function({}={}){})(0) or var {a}=0 will throw.
18

I would recommend this syntax:

function(_index, value) {...} 

to not to shadow lodash variable and still have description of argument in case if it will be used.

VS Code is also highlight these names properly and these unused args won't be deleted after autofix code smells

Comments

2

How about using the function arguments object?

function third() { const [,,thirdArg] = arguments; return thirdArg; } console.log(third(1,2,3));

1 Comment

I like the answer by @Joseph Marinier even better stackoverflow.com/questions/32197927/…
1

Another approach: For unused parameters to be ignore from unused errors using:

function(_1, _2, toBeUseParam) { ... } 

Comments

1

There isn't a "standard"/"official" syntax, but there're a few options for skipping the unused arguments (from most to least recommended):

  • Prefix its name with an underscore, e.g. "(arg1, _arg2, arg3, _arg4, arg5) => {...}"
  • Use a destructured array, e.g. "(...[, , arg3, arg4]) => {...}"
  • Destruct each into an empty object, e.g. "(arg1, {}, {}, arg4) => {...}", if it's undefined (not including null), add default assignment, e.g. "(arg1, {}={}, {}={}, arg4) => {...}"
  • Replace it with underscore(s), and increment number of underscores for each one, e.g. "(arg1, _, __, ___, arg5) => {...}"

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.