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:
Accessibility Specialist. I focus on ensuring content created, events held and company assets are as accessible as possible, for as many people as possible.
Accessibility Specialist. I focus on ensuring content created, events held and company assets are as accessible as possible, for as many people as possible.
Note the comma on the left side of the destructuring: it means that the first element is ignored. secondColor is assigned with the element at index 1 from the colors array.
That doesn’t lead in to the next section it is referencing a code snippet that is either missing (which after rereading, looks to be the case) or further down and part of section 3 but you changed the example. Either way it is definitely not right!
Or if you were being sarcastic and I missed it then feel free to r/whoosh me 😜🤣
So post increment was used in the movies example .
So it adds 1 to the operator.
So if you do index++ it will log the firstvalue.
If you do ++index it will log the secondvalue
eg
constmovies={list:[{title:'Skyfall'},{title:'Interstellar'}],[Symbol.iterator](){letindex=0;return{next:()=>{if(index<this.list.length){constvalue=this.list[index++].title;return{value,done:false};}return{done:true};}};}};const[firstMovieTitle]=movies;console.log(firstMovieTitle);// so it logs skyfall
but once we use the pre increment it will log interstellar
constmovies={list:[{title:'Skyfall'},{title:'Interstellar'}],[Symbol.iterator](){letindex=0;return{next:()=>{if(index<this.list.length){constvalue=this.list[++index].title;return{value,done:false};}return{done:true};}};}};const[firstMovieTitle]=movies;console.log(firstMovieTitle);// now logs interstellar
and if we just left it without increment then it would just be skyfall because it just logs the index.
I am a writer online, editor, and researcher specializing in educational, and domestic issues. Previously, I spent five years in marketing in the self-help and health.
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
Quick heads up - it looks like the paragraph at the end of section 2 belongs in section 3.
Great article and well explained! ❤️
Nice point out but I do like giving people a preview
That doesn’t lead in to the next section it is referencing a code snippet that is either missing (which after rereading, looks to be the case) or further down and part of section 3 but you changed the example. Either way it is definitely not right!
Or if you were being sarcastic and I missed it then feel free to r/whoosh me 😜🤣
ahahha
yeah I was being sarcastic , it was more of a trick
It was a trick to provoke thinking over again and getting to the bottom of the topic
Nice article. I have a question about the movies iterable example. Why are you using the increment operator when accessing the value?
So post increment was used in the movies example .
So it adds
1to the operator.So if you do
index++it will log the firstvalue.If you do
++indexit will log the secondvalueeg
but once we use the pre increment it will log interstellar
and if we just left it without increment then it would just be skyfall because it just logs the index.
I hope that helps.
Thanks for the explanation. So without the increment you wouldn’t be able to destructure further movies in the list, only the first one?
yes
Wow, you have some good content on there.
Thank you
Awesome, thank you :-)
Thanks for reading it 🙂
Excellent article!
Thank you