Standard conventions for indicating a function argument is unused in JavaScript

39,168

Solution 1

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 ($.each is backward relative to Array#forEach):

$.each(objectOrArrayLikeThing, function(_, value) { }
    // Use value here
});

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), 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.

Solution 2

Using destructuring assignment, one can do:

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

f(1, 2, 3);

Solution 3

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.

Solution 4

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

Share:
39,168

Related videos on Youtube

oligan
Author by

oligan

In part of my spare time, I work on fun programming projects. One was trying to analyze what underlies Wikipedia's Get to Philosophy game. I also worked on one called the "Small Eigen Collider". I'm currently learning Japanese, and I'm an active participant in lang-8.com, a website where you write journal entries in a language you're learning, and get corrected by native speakers of that language. In return, you correct people writing entries in your native language. Recently, I've been asking a few questions prompted by slightly incorrect English I've encountered on lang-8.

Updated on July 01, 2021

Comments

  • oligan
    oligan almost 3 years

    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?

    • Swaraj Giri
      Swaraj Giri over 8 years
      From what i have seen so far, its still _ that's being used.
    • Tushar
      Tushar over 8 years
      If you're looking to remove unused variables use jslint
  • Alec Mev
    Alec Mev almost 7 years
    Unfortunately, this throws if an unused argument is undefined or null.
  • oluckyman
    oluckyman over 6 years
    @OlegsJeremejevs function ({}={}, {}={}, value) { ... } helps here, but it becomes messy...
  • Alec Mev
    Alec Mev over 6 years
    @oluckyman Nice workaround! "Don't want to name an unused arg? Just dumbbell it!"
  • Hanming Zeng
    Hanming Zeng over 5 years
    you can't do function (_, _, value) though
  • T.J. Crowder
    T.J. Crowder over 5 years
    @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.
  • zrajm
    zrajm over 4 years
    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.
  • T.J. Crowder
    T.J. Crowder over 4 years
    @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.)
  • Doug Coburn
    Doug Coburn over 4 years
    I like the answer by @Joseph Marinier even better stackoverflow.com/questions/32197927/…
  • Him Hah
    Him Hah over 4 years
    This seem more simpler to me.
  • zypA13510
    zypA13510 about 4 years
    Wow, I always thought that destructuring a primitive value, i.e. (function({}={}){})(0) or var {a}=0 will throw.