Is it possible for me to abort the rest of a function depending on the condition of an if statement that's inside it without using an else condition?
Code example below.
("#btn").click(function(){
if(name == call_name){
alert(err)
}
//abort the rest of the function below without the need for else
});
Thank you.
You can use a return statement to short-circuit a function.
("#btn").click(function(){
if(name == call_name){
alert(err)
return false;
}
//abort the rest below without the need for else
});
Good point by others that in the case of an event like this you may want to return false to prevent the event bubbling and being caught by other handlers. In the general case though, return;
works fine to short-circuit the function and tends to be the clearest way to do so.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With