Created
December 29, 2022 13:43
-
-
Save DevGW/8e6e665dbbf086d0087161f3177ce437 to your computer and use it in GitHub Desktop.
Javascript :: for loop vs while loop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// loop through string | |
let letters = 'abcdefg'; | |
// initial: let i=0 | |
// conditional: i<letters.length | |
// final: i++ | |
for (let i=0; i < letters.length; i++) { | |
let currentLetter = letters[i]; | |
console.log(currentLetter); | |
} | |
// "while" loops are interchangeable but you have to remember to put the eventual false condition to avoid looping indefinitely | |
// init is outside the while loop | |
let j = 0 | |
while (j < letters.length) { | |
let currentLetter = letters[j]; | |
console.log(currentLetter); | |
j++; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment