DEV Community

Ruxin Qu
Ruxin Qu

Posted on • Edited on

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

Top comments (0)