constarr=[4,6,1,9];for(const[idx,val]ofarr.entries()){console.log(`Index "${idx}" holds "${val}"`);}/*
* Output:
* Index "0" holds "4"
* Index "1" holds "6"
* Index "2" holds "1"
* Index "3" holds "9"
*/
And there's object destructuring (which can have nesting of terms):
constperson={name:{first:"John",last:"Doe",},age:37,};// You don't have to capture all the fields.const{name:{first,last:surname},age}=person;console.log(`${first}${surname} is ${age} years old.`);
It is handy in loops as well.
Also, it can be used in parameter lists.
constpeople=[{name:"John Doe",age:37,},{name:"Mary Jane",age:14,},{name:"Edith Oldcastle",age:74,},];for(const{name,age}ofpeople){console.log(`${name} is ${age} years old`);}// This is just an example. You could just use `(total, person) => (total + person.age)`.constaverageAge=people.reduce((total,{age})=>(total+age),0)/people.length;console.log(`Average age: ${Math.round(averageAge)} years`);/*
* Output
* John Doe is 37 years old
* Mary Jane is 14 years old
* Edith Oldcastle is 74 years old
* Average age: 42 years
*/
P.S.
Could you rather use code blocks instead of/as well as "screenshots"? The "screenshots" look nice, but you can't copy-paste code from them. For this it isn't really an issue, but for anything longer it can be nice to be able to if someone wants to just copy it and test it out.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Array destructuring is good for
for
loops:And there's object destructuring (which can have nesting of terms):
It is handy in loops as well.
Also, it can be used in parameter lists.
P.S.
Could you rather use code blocks instead of/as well as "screenshots"? The "screenshots" look nice, but you can't copy-paste code from them. For this it isn't really an issue, but for anything longer it can be nice to be able to if someone wants to just copy it and test it out.