Giter Site home page Giter Site logo

what is the meaning of break about learning-area HOT 4 CLOSED

mdn avatar mdn commented on May 23, 2024
what is the meaning of break

from learning-area.

Comments (4)

chrisdavidmills avatar chrisdavidmills commented on May 23, 2024

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.

SayanChatterjee1990 avatar SayanChatterjee1990 commented on May 23, 2024

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.

athiratj avatar athiratj commented on May 23, 2024

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.

avivmu avatar avivmu commented on May 23, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.