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.
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
1
to the operator.So if you do
index++
it will log the firstvalue.If you do
++index
it 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