25

Is there a faster/more efficient way of declaring multiple variables in react native? Instead of

let foo = 'foo';
let bar = 'bar';
let foobar = 'foobar';

Obviously not problem for three variables, but for bigger sets of variables, is there a better way?

0

2 Answers 2

35

"Faster"? You've determined there's a performance bottleneck here?!

In any case, you can declare multiple variables:

let foo    = 'foo'
  , bar    = 'bar'
  , foobar = 'foobar'
  ;

But this is simply JS syntax–is this what you are really asking?

If you have a "large" number of related variables the problem may be more systemic, and there are multiple types of refactorings that might help.

Updated: I used to declare variables like this; I don't anymore.

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

5 Comments

Not really a performance bottleneck, so much as saving time not having to type 'let' more than a handful of times. And I know it's basic javascript variable declaration, but was unaware if the syntax was the same in react native / jsx, and googling really didn't turn up much.
That was a lot of extra for a very simple answer, don't you think?
@SeanLindo Oh yeah. And I could have done more. Not that I spend an inordinate amount of time thinking about syntax and code aesthetics and whatnot :D ... :/ ... :'(
hi, may I ask why you don't declare variable like this anymore?
@aanhlle Just don’t; switched to multiple let statements. At the time I wrote this I was writing more Elixir than JS.
19

This is more of a general JS syntax question.

Depending on your use case, you can just destructure an array as such:

const [ foo, bar, foobar ] = [ 'foo', 'bar', 'foobar' ]

Note that these are constants, having a lot of variables in a scope makes it poorly readable. See here for more

2 Comments

(I'm not convinced destructuring for destructuring's sake is the clearest code--it really separates the reference and its value and forces a fair amount of cognitive overhead if the number of refs is high.)
IMHO the solution is not to avoid destructuring, but to avoid the high number of refs. However, this makes destructuring much less needed.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.