SyntaxStudy
Sign Up
JavaScript Beginner 1 min read

Loops

Loops

for

Best when you know how many times.

while

Best when driven by a condition.

for...of

Cleanest for arrays/strings.

for...in

Iterates object keys.

Example
for(let i=0;i<5;i++) console.log(i);

for(const fruit of ['apple','banana']){
  console.log(fruit);
}

const user={name:'Alice',age:25};
for(const key in user){
  console.log(key,':',user[key]);
}