JavaScript: for loop VS for...of loop

rosiequ

Ruxin Qu

Posted on August 8, 2022

JavaScript: for loop VS for...of loop
  • for loop example:
dailyRoutine = ['eat','sleep','code'];
for (let x = 0; x < dailyRoutine.length; x++){
    console.log(dailyRoutine[x]);
}
Enter fullscreen mode Exit fullscreen mode

  • for...of loop example:
dailyRoutine = ['eat', 'sleep','code'];
for (const routine of dailyRoutine){
    console.log(routine);
}
Enter fullscreen mode Exit fullscreen mode

Note:

  1. The variable routine in for...of loop can be declared using var, let, or const
  2. for loop and for...of loop can be used for arrays as well as strings
  3. for(let x = array.length - 1; x >= 0; X--) can print in reverse order
  4. break and continue can control the looping
💖 💪 🙅 🚩
rosiequ
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