Comments (4)
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.
from learning-area.
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.
from learning-area.
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
from learning-area.
Why is this issue still open? @Sepharvaim / @chrisdavidmills kindly close it. I think this kind of question should be asked in the forum
from learning-area.
Related Issues (20)
- Learning area
- Left sidebar doesn't show scrollbar at specific font sizes. HOT 1
- html/forms/html-form-structure/payment-form.html STYLING ISSUE HOT 3
- Guides > Tools and testing > Cross browser testing > Handling common HTML and CSS problems (Issue)
- [revise] fix payment form code snippet HOT 1
- [type="number"]
- Test your skills: Advanced HTML text — Task 2, paragraph 2: Copy discrepancy HOT 19
- Error Message is Cut Off at the Top when the Example is at the Bottom of the Browser Window HOT 10
- javascript/introduction-to-js-1/first-splash/number-guessing-game.html line 64 is different than any material covered by the tutorial HOT 4
- Standardize lang attribute usage in HTML tutorial on MDN to 'en-US' or 'en-us' HOT 3
- Auto-generated method stub
- Auto-generated method stub
- Width capped at 90% and height capped at 160px in javascript/building-blocks/tasks/styles.css HOT 3
- Label is covered by Text Area in learning-area/html/forms/your-first-HTML-form/first-form-styled.html
- My first issue HOT 1
- new day
- Site
- Where are my divs? HOT 4
- Java script
- .table-container not contained in .main-content
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from learning-area.