The usual way to swap 2 variables requires an additional temporary variable.
Yet destructuring requires a temporary array, which my intuition tells me, is harder (not impossible, mind you) to optimise and has to be garbage-collected.
There's a much nicer way to do this without destructuring:
letfirstColor=colors[0]??"white"
or using || on older javascript versions.
const[,...fooNumbers]=numbers;
Again, there's a much more readable version of this without destructuring:
constfooNumbers=numbers.slice(1)
const{foo,...small}=big;
That's actually a really good example where destructuring is the most readable way of doing it. That being said, one could always just write a function to do a similar thing:
Is it pretty? no, not really. But at least it's possible :D
Personally, I really like using destructuring for function arguments. You might have already spotted it in my drop() example above: instead of indexing the array in the function body, I let the function declaration take care of that, and then just work with the variables I actually want in the body.
Getting many values out of a nested data structure is also really nice this way:
Yet destructuring requires a temporary array, which my intuition tells me, is harder (not impossible, mind you) to optimise and has to be garbage-collected.
There's a much nicer way to do this without destructuring:
or using
||
on older javascript versions.Again, there's a much more readable version of this without destructuring:
That's actually a really good example where destructuring is the most readable way of doing it. That being said, one could always just write a function to do a similar thing:
Is it pretty? no, not really. But at least it's possible :D
Personally, I really like using destructuring for function arguments. You might have already spotted it in my
drop()
example above: instead of indexing the array in the function body, I let the function declaration take care of that, and then just work with the variables I actually want in the body.Getting many values out of a nested data structure is also really nice this way:
To be honest I do use
||
for destructuring also.You did make some good points here thanks :)
I do also like the way you did this.
:) Thanks again