What are the ways we can break out of a loop in JavaScript?

What are the ways we can break out of a loop in JavaScript?

What are the ways we can break out of a loop in JavaScript? The continue keyword lets us skip one iteration, in the for and for..of and while loops. The loop does end that iteration, and will continue from the next one. A for..in loop can’t use break. It’s not possible to end it in this way.

Here is a for loop:

const list = ['a', 'b', 'c']
for (let i = 0; i < list.length; i++) {

}

We can break at any point in time the execution using the break keyword:

const list = ['a', 'b', 'c']
for (let i = 0; i < list.length; i++) {
  if (list[i] === 'b') break
  console.log(list[i])
}

break also works in for..of loops:

const list = ['a', 'b', 'c']
for (const item of list) {
  if (item === 'b') break
  console.log(item)
}

javascript programming loop

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

JavaScript Programming Basics - For Loops and ForEach Loop

In this JavaScript programming basics tutorial, we'll be covering the for loop and the forEach loop.

Learn While Loops In JavaScript | Beginner JavaScript Tutorial | 2020

In this video we are going to learn While Loops In Details. One of the key features of JavaScript is a loop: it makes a particular action perform repeatedly, saving you time. All JavaScript loops can be split into two categories: for loops and while loops. JavaScript for loops are again divided into three types: the general for loop, a for/in loop and a for/of loop.

For loop in C++ Program | C++ For Loop Example

For loop in C++ Program | C++ For Loop Example.

For Loops, While Loops, Do While Loops | Javascript Loop Tutorial for Beginners

In this video we'll look at several examples of Loops in Javascript and learn how to iterate through data with while, do while, and for loops.

Learning JavaScript: for Loops

In this article I’m going to cover the different forms of the for loop that are available in JavaScript. There is the general for loop, the for..in loop, and the for..of loop. I will describe how each loop works and when is the right time to use each loop type. I am leaving out the Array.forEach loop as it is specialized for arrays and requires some knowledge of functions I haven’t covered yet.