views:

54

answers:

3

I can't find a recommended way to STOP a function part way thru when a given condition is met. Something like Exit, or Break?

I am currently using this?

If x >=10 {Return;}  
other conditions;
+2  A: 

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
@g.d.d.cThanks for the confirmation. Couldn't find this answer by Googling.
Rhys
A: 

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
A: 

use return for this

if(i==1) { 
    return; //stop the execution of function
}
else {
   //keep on going
}
Starx
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
You are right.....
Starx