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 (_ => {})
asked Dec 24, 2020 at 0:27
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 _.

answered Dec 24, 2020 at 0:30
Sign up to request clarification or add additional context in comments.

1 Comment

Interesting, so it's basically just an unused placeholder parameter. Thanks for the info!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.