JavaScript Loops: There is More than ‘for’

I’m bored of basic for statements…

Found it!

Loops

Our Loop

for statement

let arr = [1, 2, 3, 4, 5];for (let i = 0; i < arr.length; i++) {
console.log(arr[i]);
}
// 1,2,3,4,5
-> undefined

do…while statement

let arr = [1, 2, 3, 4, 5];
let i = -1;
do {
i += 1;
console.log(arr[i]);
} while (i < arr.length - 1);
// 1,2,3,4,5
-> undefined

while statement

let arr = [1, 2, 3, 4, 5];
let i = -1;
while (i < arr.length - 1) {
i += 1;
console.log(arr[i]);
}
// 1,2,3,4,5
-> undefined

for…of statement

let arr = [1, 2, 3, 4, 5];for (let i of arr) {
console.log(i);
}
// 1,2,3,4,5
-> undefined

for…in statement

let arr = [1, 2, 3, 4, 5];for (let i in arr) {
console.log(arr[i]);
}
// 1,2,3,4,5
-> undefined

Software Engineer hailing from the Empire State, writing about what interests me and hoping someone else finds it interesting too. 👨🏻‍💻 🤓 He/Him #LFGM

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store