Loops: for, for...of, forEach

Pick the simplest loop for the job.

JavaScript · Loops: for, for...of, forEachBeginner
const fruits = ['apple', 'banana', 'cherry'];
const seen = [];

for (let i = 0; i < fruits.length; i++) { seen.push(i + ':' + fruits[i]); }

for (const f of fruits) { seen.push(f.toUpperCase()); }                 // values

for (const [i, f] of fruits.entries()) { seen.push(`${i + 1}.${f}`); }  // index + value

const user = { name: 'Ali', age: 30 };
for (const [key, value] of Object.entries(user)) { seen.push(key + '=' + value); }

fruits.forEach((f, i) => seen.push(f[0]));
loopforfor offorEach