Things to remember about JavaScript ForEach Loop

Things to remember about JavaScript ForEach Loop

·

1 min read

Table of contents

No heading

No headings in the article.

Can’t Skip an Iteration with "Continue"

const arr = [1, 2, 3, 4, 5, 6, 7]

arr.forEach((val) => {
    if (val === 3) {
        continue;
    }
    console.log("current: ", val)
}
)
// SyntaxError: Unsyntactic continue

Can’t End A Loop Early with "Break"

const arr = [1, 2, 3, 4, 5, 6, 7]

arr.forEach((val) => {
    if (val === 3) {
        break;
    }
    console.log("current: ", val)
}
)
// SyntaxError: Unsyntactic break

Always Returns "Undefined"

const arr = [1, 2, 3, 4, 5, 6, 7]

arr.forEach((val) => {
    if (val === 3) {
        return val;
    }
}
)
//Undefined