"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > JavaScript For Loop Examples

JavaScript For Loop Examples

Published on 2024-11-08
Browse:133

JavaScript For Loop Examples

Standard For Loop

for (let i = 0; i 



For...of Loop (Iterating over Arrays)

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

For...in Loop (Iterating over Object Properties)

const person = {
  name: 'John',
  age: 30,
  job: 'Developer'
};

for (const key in person) {
  console.log(`${key}: ${person[key]}`);
}

forEach Loop (Array Method)

const numbers = [1, 2, 3, 4, 5];
numbers.forEach((number, index) => {
  console.log(`Index ${index}: ${number}`);
});

While Loop (Not technically a for loop, but worth mentioning)

let count = 0;
while (count 



These examples demonstrate various ways to iterate in JavaScript, each suited for different scenarios and data structures.

Release Statement This article is reproduced at: https://dev.to/turck/javascript-for-loop-examples-5a10?1 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3