22

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

7
  • _ is used by the underscore library in JS. So, better to use dummy or something else. Commented Jan 6, 2016 at 8:42
  • @AJK: ooops, sure, you are right .. Commented Jan 6, 2016 at 8:43
  • 3
    I've never used a "throwaway variable" - so it'd be a cows opinion Commented Jan 6, 2016 at 8:43
  • @JaromandaX: an example here: stackoverflow.com/q/34628857/647991 Commented Jan 6, 2016 at 8:47
  • @gonvaled I think you should accept my answer if it answers your question, otherwise this question will remain in the "unanswered" section. Commented Jan 7, 2016 at 8:12

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

7

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.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.