This guide can help you learn how to solve the SyntaxError: Illegal break statement in JavaScript. Follow this article to learn more about it with the explanation and examples below.
How does the SyntaxError: Illegal break statement in JavaScript happen?
The SyntaxError: Illegal break statement in JavaScript occurs when you misuse the break statement. For example, you try to use the break statement in the function, or you use it outside the loop. If you do like that, this error will occur.
Look at the example to learn more about this error.
function example(){
let result = ''
const arr = ['Learn', 'Share', 'IT', 'crvt4722']
arr.forEach(str => {
if (str == 'crvt4722') {
break // The Error: SyntaxError: Illegal break statement will occur here
}
console.log(str)
})
}
example()
Output
SyntaxError: Illegal break statement
In this example, this error occurs because you use the break statement in the function in the for loop.
How to solve this error?
These are some solutions that can help you solve the SyntaxError: Illegal break statement. If you want to terminate the function when it meets a specific condition, you can use the return statement instead. Or if you are using the forEach() function to iterate the array, you can use the ‘for … of’ loop instead. Another solution is using the try-catch method.
Use the return statement instead
If you want to terminate the function when it meets a specific condition, you can use the return statement instead.
Look at the example below to learn more about this solution.
function example(){
const arr = ['Learn', 'Share', 'IT', 'crvt4722']
arr.forEach(str => {
if (str == 'crvt4722') {
// If the String in the Array is equal to 'crvt4722'. The function will terminate.
return
}
console.log(str)
})
}
example()
Output
Learn
Share
IT
Use the ‘for … of’ loop
If you use the forEach() method and break inside it in the function, the error will occur. Try to use the ‘for … of’ loop instead.
Look at the example below to learn more about this solution.
function example(){
const arr = ['Learn', 'Share', 'IT', 'crvt4722']
for (let str of arr){
if (str == 'crvt4722') {
// If the String in the Array is equal to 'crvt4722'. The loop will terminate
break
}
console.log(str)
}
}
example()
Output
Learn
Share
IT
Use the try-catch method
You can use the try-catch method instead to break the loop.
Look at the example below to learn more about this solution.
function example(){
const arr = ['Learn', 'Share', 'IT', 'crvt4722']
try {
arr.forEach(str => {
if (str == 'crvt4722') {
// If the String in the Array is equal to 'crvt4722'. The loop will terminate.
throw SyntaxError
}
console.log(str)
})
} catch (error){
if (error != SyntaxError) throw error
}
}
example()
Output
Learn
Share
IT
Summary
To solve the SyntaxError: Illegal break statement in JavaScript, you can use the return statement, the ‘for … of’ loop, or the try-catch method instead of the break statement. Choose the solution that is best for you. We hope this tutorial is helpful to you. Thanks!
Maybe you are interested:
- TypeError: $(…).on is not a function in jQuery
- “SyntaxError: Illegal return statement” in JavaScript
- SyntaxError: Unexpected token in JavaScript

My name is Thomas Valen. As a software developer, I am well-versed in programming languages. Don’t worry if you’re having trouble with the C, C++, Java, Python, JavaScript, or R programming languages. I’m here to assist you!
Name of the university: PTIT
Major: IT
Programming Languages: C, C++, Java, Python, JavaScript, R