Title: JavaScript illegal break statement?
Tags: javascript
Question: All I have is
if (arr.length == 0)
{
document.getElementById("input_table").innerHTML += "<p&gt;Can't sort an empty array, bro!</p&gt;";
break;
}
and I'm getting an "Uncaught SyntaxError: Illegal break statement" error from my Chrome console.
I don't think I need to post my entire code, since context is irrelevant here.
Comment: If you are trying to return early, change the `break` to a `return`. Otherwise, just remove the `break`.

Comment: We need the surrounding code for context.

Comment: `break` is only valid within a `switch > case` context or a loop (e.g `for` or `while`)

Comment: `return` may not be valid either, as it only applies within a context of a closure (e.g. a `function`). Soo.. you said the context is irrelevant, but actually the context **is** relevant.

Comment: No need for the downvote. There's errors in the code, yes, but the question itself is fine.


Here is another answer: You need to remove the break completely. Others have stated to use return instead, but that's a bit misleading. You don't need anything, only your logic to tell when to exit (or the closing bracket).

Here is another answer: Break is for loops or switch statements, try a return instead.
Comment for this answer: Yes, you're both right
Comment for this answer: also worth noting return is not necessary in this example
Comment for this answer: This assumes he's inside a closure to be returned from.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 5, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2014:00:03/28/14', 'FavoriteCount': 1.0, 'PostTypeId': 1, 'Tags': '<javascript>', 'Title': 'JavaScript illegal break statement?', 'ViewCount': 14488, 'id': 22724632, 'user_AboutMe': None, 'user_AccountId': 4227915.0, 'user_DisplayName': 'user3461018', 'user_Reputation': 139.0, 'user_Views': 18.0, 'user_id': 3461018.0}}