Preface
There are many traversal methods in JavaScript. Today, I will mainly summarize how these traversal methods break out of the loop. Welcome all the great masters and colleagues to give advice and correct them 😁
First of all, it is necessary to know:
- Return must be used in the function
- Return has 2 functions, ending the function and returning the result
There are the following ways to jump out of the loop in JS
Method Break out of the loop
- break jumps out of the current loop, and the code behind the loop can still be executed
- return can terminate the current function, and the code after the loop cannot be executed
- continue Skip the loop and execute the subsequent loop
function a(){ var arr = [1,3,5,7,9]; var id = 5; for (var i = 0; i < ; i++) { if(arr[i]=== 1) continue; //Skip this loop (arr[i]) if (arr[i] === id) { break; //Satisfies the condition and jump out of the loop; the code behind the loop can still be executed } if (arr[i] === 6) { return; //The current function can be terminated as the condition is met } } }
Method breaks out of the loop
- Can't use break, continue to jump out of the traversal, because foreach is executed asynchronously, the code may have been executed before break
- ForEach() itself cannot jump out of the loop, and it must traverse all the data to end
- Break out of the loop by throwing an exception to achieve break effect
// In forEach, it is impossible to end the traversal before the traversal is over with break functions. If you want to terminate early, you must put the forEach() method in a try block and throw an exception.var arr = [1,3,5,7,9]; var id = 5; try { (function (curItem, i) { if(curItem === 1) return; (curItem) if (curItem === id) { throw Error(); //Satisfies the conditions and breaks out of the loop } }) } catch (e) { }
- Skip the loop by returning to achieve the effect of continuing
(function (curItem, i) { if(curItem === 1) return; (curItem) if (curItem === id) { return; //Satisfies the conditions and breaks out of the loop // break grammar error; } })
3. map() jumps out of the loop
- map, like forEach, cannot break out of loop through break.
- The difference between map() and forEach()
// The former does not change the content of the original array // The latter changes the content of the original array
4. for of
- continue
for (let i of arr) { if (i === 3) { continue } (i) }
- break break
for (let i of arr) { if (i === 3) { break } (i) }
- return can terminate the current function, and the code after the loop cannot be executed
var a=[1,2,3,4]; function b(){ for(var key of a){ if(key=2){ return false }; ('Hahaha')} }; (b()) //false
5. while do while jump out of the loop
- continue
let j = 1 while (j < 6) { if (j === 3) { j++ continue } (j) // 1 2 4 5 j++ }
- break break
var i=0; while (i < 6) { if (i === 3) { i++ break } (i) // 0,1,2 i++ }
- return can terminate the current function, and the code after the loop cannot be executed
var i=0; while (i < 6) { if (i === 3) { return } (i) i++ }
Every traversal
- Every can control whether to continue traversal by returning the bool value.
let arr = [1, 2, 3, 4, 5] (i => { (i) return true //1,2,3,4,5 }) (i => { (i) if (i === 3) { return false //1,2,3 } else { return true } })
Some traversal
- Some can end the traversal by controlling return true.
- If return is not written, the last value will be traversed and true will be closed by default
let arr = [1, 2, 3, 4, 5] (i => { (i) // 1,2,3,4,5 }) (i => { (i) if (i === 3) { return true //1,2,3 } }) }
Summarize
This is the article about the summary of the method of traversing out of loops in JavaScript. For more related contents of traversing out of loops, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!