Skip to content
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

Closed
Sepharvaim opened this issue Nov 5, 2019 · 4 comments
Closed

what is the meaning of break #164

Sepharvaim opened this issue Nov 5, 2019 · 4 comments

Comments

@Sepharvaim
Copy link

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?

@Sepharvaim Sepharvaim changed the title what the meaning of break what is the meaning of break Nov 5, 2019
@chrisdavidmills
Copy link
Contributor

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.

@SayanChatterjee1990
Copy link

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.

@athiratj
Copy link

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.
In this example we use a for loop together with the break statement.

Loop through a block of code, but exit the loop when the variable i is equal to "3":

var text = ""
var i;
for (i = 0; i < 5; i++) {
if (i === 3) {
break;
}
text += "The number is " + i + "
";
}

Output:
The number is 0
The number is 1
The number is 2

@avivmu
Copy link
Contributor

avivmu commented May 23, 2020

Why is this issue still open? @Sepharvaim / @chrisdavidmills kindly close it. I think this kind of question should be asked in the forum

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants