JavaScript Break and Continue | its use in Labels

Опубликовано: 26 Октябрь 2024
на канале: ArvindProgramming
27
like

JavaScript Break and Continue | its use in Labels @arvindprogramming
The break statement in JavaScript is used to terminate the loop or switch statement and transfer the control to the statement just after the terminated loop or switch. The continue statement, on the other hand, is used to skip the current iteration of the loop and move to the next iteration.

Here is an example of how to use break and continue in JavaScript:

Using break:
```javascript
for (let i = 1; i less than= 10; i++) {
if (i === 5) {
break; // terminate the loop if i is equal to 5
}
console.log(i);
}
// Output: 1 2 3 4

```

Using continue:
```javascript
for (let i = 1; i less than= 10; i++) {
if (i === 5) {
continue; // skip the current iteration if i is equal to 5
}
console.log(i);
}
// Output: 1 2 3 4 6 7 8 9 10
```

In the above examples, the break statement is used to terminate the loop when the value of `i` becomes 5. Therefore, the loop stops executing at that point. The continue statement is used to skip the iteration when the value of `i` becomes 5. Therefore, it skips the console.log statement and moves to the next iteration.

It's important to note that break and continue statements only work within loops (for, while, do-while) and switch statements in JavaScript.
Social Media Links-
Facebook Page --   / arvindweb   Coaching-Programming & Games/
Facebook -   / arvindweb  
Twitter -   / arvindwebspn  
YouTube -    / @arvindprogramming  
Instagram -   / arvindprogramming