Conclusion first: In for, for-in, for-of, break and continue allow normal execution and desired results, but return does not. In forEach, Map, and filter, break and continue are broken, and a return breaks out of the current loop but continues into the next loop.

The for loop

1. Use break
let array = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
for(let i = 0; i < array.length; i++) {
    if (5 == i) {
        break;
    }
    console.log(array[i]);
}
Copy the code

2. Continue to use
let array = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
for(let i = 0; i < array.length; i++) {
    if (5 == i) {
        continue;
    }
    console.log(array[i]);
}
Copy the code

3. Use return
let array = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
for(let i = 0; i < array.length; i++) {
    if (5 == i) {
        return;
    }
    console.log(array[i]);
}
Copy the code

Conclusion: Break and continue execute normally in for, not return.

Second, for-in loop

1. Use break
let array = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
for(let i in array) {
    if (5 == i) {
        break;
    }
    console.log(array[i]);
}
Copy the code

2. Continue to use
let array = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
for(let i in array) {
    if (5 == i) {
        continue;
    }
    console.log(array[i]);
}
Copy the code

3. Use return
let array = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
for(let i in array) {
    if (5 == i) {
        return;
    }
    console.log(array[i]);
}
Copy the code

Conclusion: Break and continue execute normally in for-in, return does not.

3. For-of loop

1. Use break
let array = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
for(let i of array) {
    if (5 == i) {
        break;
    }
    console.log(array[i]);
}
Copy the code

2. Continue to use
let array = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
for(let i of array) {
    if (5 == i) {
        continue;
    }
    console.log(array[i]);
}
Copy the code

3. Use return
let array = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
for(let i of array) {
    if (5 === i) {
        return;
    }
    console.log(array[i]);
}
Copy the code

Conclusion: Break and continue execute normally in for-in, return does not.

ForEach loop

1. Use break
let array = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
array.forEach((item) => {
    if (5 == item) {
        break;
    }
    console.log(item);
});
Copy the code

2. Continue to use
let array = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
array.forEach((item) => {
    if (5 == item) {
        continue;
    }
    console.log(item);
});
Copy the code

3. Use return
let array = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
array.forEach((item) => {
    if (5 == item) {
        return;
    } else{ console.log(item); }});Copy the code

Conclusion: Break and continue do not execute normally in forEach. Exceptions occur and a return breaks out of the current loop, but does not terminate the loop.

5. Filter loop

1. Use break
let array = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
let res = [];
res = array.filter((item) => {
    if (5 == item) {
        break;
    }
    return item;
});
console.log(res);
Copy the code

2. Continue to use
let array = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
let res = [];
res = array.filter((item) => {
    if (5 == item) {
        break;
    }
    return item;
});
console.log(res);
Copy the code

3. Use return
let array = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
let res = [];
res = array.filter((item) => {
    if (5 == item) {
        return;
    }
    return item;
});
console.log(res);
Copy the code

Conclusion: Break and continue cannot be executed normally in filter. An exception occurs, and a return breaks out of the current loop by removing the current position and changing the length of the array, but does not terminate the loop.

Map loop

1. Use break
let array = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
let res = [];
res = array.map((item) => {
    if (5 == item) {
        break;
    }
    return item;
});
console.log(res);
Copy the code

2. Continue to use
let array = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
let res = [];
res = array.map((item) => {
    if (5 == item) {
        break;
    }
    return item;
});
console.log(res);
Copy the code

3. Use return
let array = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
let res = [];
res = array.map((item) => {
    if (5 == item) {
        return;
    }
    return item;
});
console.log(res);
Copy the code

Conclusion: Break and continue cannot be executed normally in filter. An exception will occur and return will break out of the current loop. The current position will be replaced by undefined.