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