Different ways to use for loops in Node.js

Different ways to use for loops in Node.js
Reading Time: < 1 minute

For many developers, Node.js can be an overwhelming and daunting technology to learn. Fortunately, one simple way to ease into the Node.js world is through the use of for loops. For loops allow you to iterate or repeat certain tasks or code until a certain condition is met. Here we will look at three different types of for loops available in Node.js and how they can be used in your Node.js applications.

Here are three examples of for loops in Node.js:

  1. Standard For Loop:
for (let i = 0; i < 5; i++) {
  console.log('Iteration:', i);

In this example, the loop iterates from 0 to 4. During each iteration, the value of i is printed to the console.

  1. For…of Loop:
const fruits = ['apple', 'banana', 'orange'];
for (const fruit of fruits) {
  console.log('Fruit:', fruit);

In this example, the loop iterates over each element in the fruits array. The value of fruit represents the current element in each iteration.

  1. For…in Loop:
const person = {
  name: 'John',
  age: 30,
  city: 'New York'
for (const key in person) {
  console.log(key + ':', person[key]);

In this example, the loop iterates over the properties of the person object. The key variable represents each property name, and person[key] retrieves the corresponding value.

These are just a few examples of the for loop variations you can use in Node.js to iterate and perform tasks.

, , ,

About the author

Andrés Canavesi
Andrés Canavesi

Software Engineer with 15+ experience in software development, specialized in Salesforce, Java and Node.js.

Join 22 other subscribers

Leave a Reply