Home › JavaScript Tutorial › JS iterations
JS iterations
More ways to loop.
forEach for arrays, for...in for object keys, Object.entries for pairs, and entries() for index + value.
Example code
<pre id='o'></pre>
<script>
let t='';const p={a:1,b:2};
[10,20].forEach((v,i)=>t+=i+':'+v+' ');
for(const k in p)t+='\n'+k+'='+p[k];
for(const [i,v] of ['x','y'].entries())t+='\n'+i+v;
o.textContent=t;
</script>