How to Exit and Stop a for Loop in Node JS?

30-Jul-2022

.

Admin

How to Exit and Stop a for Loop in Node JS?

Hi dev,

This is a short guide on how to exit and stop a for loop in node js. you can see the exit and stop a for loop in node.js. This post will give you a simple example of node js for a loop. Here you will learn for loop in node js. You just need to do some steps to do how to use for loop in node.js.

In this example how to exit and stop a for loop in node js. This example is used to exit and stop a for loop of the array list.

let's see below simple example with output:

Install Node JS


This step is not required; however, if you have not created the node js app, then you may go ahead and execute the below command:

mkdir my-app

cd my-app

npm init

The break statement terminates an active loop and proceeds your code with statements following the loop:

index.js

// create user name in array list

users = [

{ id: 1, name: 'Piyush' },

{ id: 2, name: 'Vivek' },

{ id: 3, name: 'Vishal' }

]

for (user of users) {

// exits the loop early

if (user.id == 3) {

break

}

console.log(user)

}

Output:

{ id: 1, name: 'Piyush' }

{ id: 2, name: 'Vivek' }

It will help you...

#Node JS