Skip to main content

Command Palette

Search for a command to run...

Things to remember about JavaScript ForEach Loop

Updated
•1 min read•View as Markdown
Things to remember about JavaScript ForEach Loop

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

More from this blog

Sravanth

10 posts