20

In python _ is used as a throwaway variable. Is there an idiomatic accepted name in javascript?

Community
  • 1
  • 1
blueFast
  • 41,341
  • 63
  • 198
  • 344
  • _ is used by the underscore library in JS. So, better to use `dummy` or something else. – AbdealiLoKo Jan 06 '16 at 08:42
  • @AJK: ooops, sure, you are right .. – blueFast Jan 06 '16 at 08:43
  • 3
    I've never used a "throwaway variable" - so it'd be a cows opinion – Jaromanda X Jan 06 '16 at 08:43
  • @JaromandaX: an example here: http://stackoverflow.com/q/34628857/647991 – blueFast Jan 06 '16 at 08:47
  • @gonvaled I think you should accept my answer if it answers your question, otherwise this question will remain in the "unanswered" section. – julian Jan 07 '16 at 08:12
  • @Israelg99: yes sure, sorry. You could also upvote my question, since it was interesting enough for you to answer. I wonder why questions get upvoted so rarely, even by people answering them ... A good answer is nothing without a good question :) Anyway, sorry for the short rant. I proceed to accept your answer. – blueFast Jan 07 '16 at 08:28
  • @gonvaled You are right! people tend to forget to upvote questions, I just did! :) – julian Jan 07 '16 at 15:13

3 Answers3

19

Rigth now, you can use array destructuring, no need for a var.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment

For example:

[,b] = [1,2];
console.log(b);

will output :

2

And the value 1 won't be assigned to any unused var.

Maxime Ancelin
  • 870
  • 9
  • 11
6

There isn't an accepted throwaway variable naming in JavaScript like in Python.
Although, generally in the software engineering world, engineers tend to call those variables:

dummy, temp, trash, black_hole, garbage.

Use these variable namings or whatever name that will imply to the engineer reading your code that these variables have no specific use.

With that in mind, I would strongly not recommend using signs such as below as a throwaway variable:

_, $

Unless there is an explicit convention like in Python, don't use these signs as a throwaway variable, otherwise, it may interfere with other naming conventions and libraries in other languages, like the underscore and the jQuery libraries in JS.

And of course, if you find a specific use for that variable later on, make sure to rename it.

julian
  • 4,634
  • 10
  • 42
  • 59
2

Generally, you will declare the signature of a function as an array. Simply omit one of them

const foo = (bar,baz,) => bar+baz;
foo(3,4,5) // → 7

This obviously works as long as the declaration of function argument is the last one; unlike this answer, which is valid for already-declared variables embedded in arrays. This:

const foo = (, bar,baz) => bar+baz;

will fail with:

Uncaught SyntaxError: expected expression, got ','

You can hack your way into this using any expression. However, you'll need to use the self same expression in every invocation

const quux = ([], bar,baz) => bar+baz;
quux(42,3,4); // → Uncaught TypeError: (destructured parameter) is not iterable
quux([],3,4); // → 7

That constant can be anything, so you can as well call it "dummy", but you will still have to repeat in every invocation.

jjmerelo
  • 22,578
  • 8
  • 40
  • 86