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?
You can declare multiple variables in a single line. However, there are more efficient ways to define numerous variables in JavaScript. First, define the variables' names and values separated by commas using just one variable keyword from var , let or const.
4 Ways to Declare a JavaScript Variable: Using var. Using let. Using const. Using nothing.
Do not declare more than one variable per declaration. Every declaration should be for a single variable, on its own line, with an explanatory comment about the role of the variable. Declaring multiple variables in a single declaration can cause confusion regarding the types of the variables and their initial values.
"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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With