86

I can't find a recommended way to stop a function part way when a given condition is met. Should I use something like exit or break?

I am currently using this:

if ( x >= 10 ) { return; }  
// other conditions;
user5305519
  • 3,008
  • 4
  • 26
  • 44
Rhys
  • 975
  • 1
  • 6
  • 12

7 Answers7

102

Return is how you exit out of a function body. You are using the correct approach.

I suppose, depending on how your application is structured, you could also use throw. That would typically require that your calls to your function are wrapped in a try / catch block.

g.d.d.c
  • 46,865
  • 9
  • 101
  • 111
  • Thanks for the confirmation. Couldn't find this answer by Googling. – Rhys Aug 21 '10 at 03:08
  • 8
    @Wolle - you'll notice I both listed it as an _alternative_ and with the caveat that calls to the function would need to be wrapped in a try / catch block. Depending on the function, the project scope, and what the function accomplishes, raising an exception to exit might be perfectly appropriate. One can't know without in depth knowledge of the OP's implementation. Either way, my answer was to use `return`, not `throw`. – g.d.d.c Jul 29 '14 at 14:43
  • Throwing an exception to exit is a very viable option. For example, calling a function without a valid variable will throw an `ReferenceError` exception. I have setter functions in classes that can only take values from `0` to `100` and if the value is outside of that range I throw a `RangeError` exception. Exceptions are meant to stop flow when something happened that the function was not expecting. An *exception* to the rules. – Intervalia Dec 29 '17 at 00:09
42

use return for this

if(i==1) { 
    return; //stop the execution of function
}

//keep on going
Yohaï-Eliel Berreby
  • 1,165
  • 3
  • 14
  • 25
Starx
  • 77,474
  • 47
  • 185
  • 261
  • 1
    Returning false only makes sense if you're expecting a boolean return and will return true in other situations. He might return an array value, or a status indicator, or a hint as to how far through the function he made it as the result of the conditional. – g.d.d.c Aug 21 '10 at 02:59
15

The return statement exits a function from anywhere within the function:

function something(x)
{
    if (x >= 10)
        // this leaves the function if x is at least 10.
        return;

    // this message displays only if x is less than 10.
    alert ("x is less than 10!");
}
Timwi
  • 65,159
  • 33
  • 165
  • 230
11

Use a try...catch statement in your main function and whenever you want to stop the function just use:

throw new Error("Stopping the function!");
Gordon Davisson
  • 118,432
  • 16
  • 123
  • 151
Rahul Munjal
  • 2,551
  • 2
  • 15
  • 35
1

throwing the exception when the condition is met to break the function.

function foo() {
try {
   
    if (xyz = null) //condition
        throw new Error("exiting the function foo");

} catch (e) {
    // TODO: handle the exception here
}

}

loakesh bachhu
  • 323
  • 3
  • 4
0

Try using a return statement. It works best. It stops the function when the condition is met.

function anything() {
    var get = document.getElementsByClassName("text ").value;
    if (get == null) {
        alert("Please put in your name");
    }

    return;

    var random = Math.floor(Math.random() * 100) + 1;
    console.log(random);
}
Alok Swain
  • 6,409
  • 5
  • 36
  • 57
Spidy
  • 556
  • 5
  • 11
0
if (OK === guestList[3]) {
    alert("Welcome");
    script.stop;
}
harry
  • 1
  • I found this when i was messing around with my code. Also you don't need to say script before it you can say anything but i like to say script because it makes sense. – harry Dec 10 '21 at 13:31