3

I tried looking this up and did not find an answer. Is there any reason to use _ over () when passing in 0 parameters in an arrow function? Just asking out of curiosity.

// ...(() => {}) vs (_ => {})
2
  • 2
    I dont think _ is 0 parameters. It means there is an argument but you are ignoring it. Commented Dec 24, 2020 at 0:30
  • Very weird....saves a character I suppose. Commented Dec 24, 2020 at 0:30

1 Answer 1

5

The differences are:

  • _ creates an identifier for the _ variable. (In rare circumstances, this may be confusing if one is using a library which assigns to window._, like underscore.js)
  • Using _ means that parentheses aren't needed. (In contrast, declaring a function with zero arguments requires an empty parameter list with ()) Some like saving on a character by using _ instead.

(One could have, equivalently, used any other argument name like z which then goes unused - but the convention for an unused variable is to use _)

If one isn't using a library that assigns to window._, then the _ parameter won't shadow it, so both options work just fine. Feel free to choose whichever strikes your fancy.

Note that a common linting rule forbids the declaration of unused parameters, and will require () instead of _.

1
  • Interesting, so it's basically just an unused placeholder parameter. Thanks for the info!
    – Elitezen
    Commented Dec 24, 2020 at 0:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.