-
Notifications
You must be signed in to change notification settings - Fork 26.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
what is the meaning of break #164
Comments
So basically here the break is saying "if you've found the contact you need, stop running the code". It stops the browser from wasting the effort looking through all the rest of the contacts when you've already returned the result you want. Without the break statement, it sort of works, but the trouble is that without it continues to check every contact, and if the last contact doesn't match the search name you entered, it'll give you a final message saying contact not found, even if it found it earlier on. |
The 'Break' statement allows you to have better control over your code. While executing the code when 'Break' is encountered the program stops immediately and in case of your loop('Break' can be used in any part of code) the program will come out of it. If you don't include 'Break' the program will continue executing past your desired break point. So, just as it has been mentioned by chrisdavidmills It will produce wrong output. Moreover the program will take a little more long time to run. |
The break statement exits a switch statement or a loop (for, for ... in, while, do ... while). When the break statement is used with a switch statement, it breaks out of the switch block. This will stop the execution of more execution of code and/or case testing inside the block. When the break statement is used in a loop, it breaks the loop and continues executing the code after the loop (if any). The break statement can also be used with an optional label reference, to "jump out" of any JavaScript code block. Loop through a block of code, but exit the loop when the variable i is equal to "3": var text = "" Output: |
Why is this issue still open? @Sepharvaim / @chrisdavidmills kindly close it. I think this kind of question should be asked in the forum |
I was studying loops on https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Looping_code and they say you can exit a loop with break but if I remove the break statement on the code, the code works just the same. So what's the purpose of brek in the example?
The text was updated successfully, but these errors were encountered: