10

Ignore fist parameter of function if unused with a simple _ (underscore)

Tuesday, November 10, 2020

If you ever find yourself in a situation where you need to implement function receiving multiple parameters, but you don't really need all of them, know that there is a way how to get rid of the unused parameter warning/error. It can be as simple as using an underscore as prefix or even whole name of parameter _.

There is even a way how to ignore first n params, but that might be too much, you decide.

Code Examples

callback fn with first parameter ignored

const callbackFn = (_, item) => item.id

callback fn with first parameter ignored and second one destructed to what we actually need

const callbackFn = (_, {id}) => id

callback fn with unused first parameter

const callbackFn = (index, item) => item.id

callback fn with unreasonably used parameter to avoid the warning/error

const callbackFn = (index, item) => {
  console.log(index);
  return item.id;
}

Rules

Vojtech Mašek

I am a software engineer focusing on front-end with expertise in Angular and TypeScript. I'm with Angular since its alpha phase. I'm also a Head of engineering and co-founder of FlowUp, a software development agency, where I lead multiple agile development teams.

Have a question or comment?