JavaScript: for loop VS for...of loop
Ruxin Qu
Posted on August 8, 2022
- for loop example:
dailyRoutine = ['eat','sleep','code'];
for (let x = 0; x < dailyRoutine.length; x++){
console.log(dailyRoutine[x]);
}
- for...of loop example:
dailyRoutine = ['eat', 'sleep','code'];
for (const routine of dailyRoutine){
console.log(routine);
}
Note:
- The variable routine in for...of loop can be declared using var, let, or const
- for loop and for...of loop can be used for arrays as well as strings
- for(let x = array.length - 1; x >= 0; X--) can print in reverse order
- break and continue can control the looping
💖 💪 🙅 🚩
Ruxin Qu
Posted on August 8, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
webdev A Tale of WeakMap and WeakSet in JavaScript: The Guardians of Forgotten Secrets
November 29, 2024