How to Exit and Stop a for Loop in Node JS?
Jul 29, 2022
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...
- How to Install Node Js and NPM Ubuntu?
- How to used Google ReCaptcha V3 in Node Js Express?
- How to Send Email with Attachment in Node js?
- How to Insert Data from Form into MySQL Database using Node js Express?
- Install Node Js and NPM Ubuntu 18.04/20.04/22.04 Tutorial
- How to Delete Data by id into MongoDB in Node JS?
- Install Node.js 16/17/18 on CentOS 8 Tutorial
- How to Upload File in MongoDB using Node js Express?