Skip to content

Instantly share code, notes, and snippets.

@DevGW
Created December 29, 2022 13:43
Show Gist options
  • Save DevGW/8e6e665dbbf086d0087161f3177ce437 to your computer and use it in GitHub Desktop.
Save DevGW/8e6e665dbbf086d0087161f3177ce437 to your computer and use it in GitHub Desktop.
Javascript :: for loop vs while loop
// 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