I have a function:
function myfunction() {
if (a == 'stop') // How can I stop the function here?
}
Is there something like exit()
in JavaScript?
You can just use return
.
function myfunction() {
if(a == 'stop')
return;
}
This will send a return value of undefined
to whatever called the function.
var x = myfunction();
console.log( x ); // console shows undefined
Of course, you can specify a different return value. Whatever value is returned will be logged to the console using the above example.
return false;
return true;
return "some string";
return 12345;
undefined
, and that's what my primary solution provides. The article you referenced is talking about doing return false
in a jQuery event handler. That's an entirely different issue. This is the proper way to exit a JavaScript function. Obviously if the caller relies on the value returned, the value needs to be defined appropriately.
Jul 14, 2011 at 22:03
if(a != 'stop') { /* run my code */ }
so that the code only runs when a
doesn't equal 'stop'
without providing an explicit return
. But the return value is identical to my solution. In both cases, undefined
will be returned.
Jul 14, 2011 at 22:06
undefined
(or it doesn't receive some other value) as a return value, then you'll need to conform to the specification of that API, and return the correct value. Generally for an event handling system, it will expect undefined
as a simple indication that the handler has finished, and there are no further instructions. With jQuery, return false;
has special meaning giving instruction to do a preventDefault
and stopPropagation
.
Jul 14, 2011 at 22:19
Apparently you can do this:
function myFunction() {myFunction:{
console.log('i get executed');
break myFunction;
console.log('i do not get executed');
}}
See block scopes through the use of a label: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/label
I can't see any downsides yet. But it doesn't seem like a common use.
Derived this answer: JavaScript equivalent of PHP’s die
function myfunction() {
if(a == 'stop')
return false;
}
return false;
is much better than just return;
false
better? I'd say the default undefined
is better in the generic case. Either way, you're correct to say it's often better to return a meaningful value.
Apr 30, 2013 at 15:06
false
than undefined
.
Oct 1, 2014 at 4:06
This:
function myfunction()
{
if (a == 'stop') // How can I stop working of function here?
{
return;
}
}
Using a little different approach, you can use try catch
, with throw statement.
function name() {
try {
...
//get out of here
if (a == 'stop')
throw "exit";
...
} catch (e) {
// TODO: handle exception
}
}
if you are looking for a script to avoid submitting form when some errors found, this method should work
function verifyData(){
if (document.MyForm.FormInput.value.length == "") {
alert("Write something!");
}
else {
document.MyForm.submit();
}
}
change the Submit Button type to "button"
<input value="Save" type="button" onClick="verifyData()">
hope this help.
Using a return
will stop the function and return undefined
, or the value that you specify with the return command.
function myfunction(){
if(a=="stop"){
//return undefined;
return; /** Or return "Hello" or any other value */
}
}
I think throw a new error is good approach to stop execution rather than just return or return false. For ex. I am validating a number of files that I only allow max five files for upload in separate function.
validateMaxNumber: function(length) {
if (5 >= length) {
// Continue execution
}
// Flash error message and stop execution
// Can't stop execution by return or return false statement;
let message = "No more than " + this.maxNumber + " File is allowed";
throw new Error(message);
}
But I am calling this function from main flow function as
handleFilesUpload() {
let files = document.getElementById("myFile").files;
this.validateMaxNumber(files.length);
}
In the above example I can't stop execution unless I throw new Error.Just return or return false only works if you are in main function of execution otherwise it doesn't work.
throw new Error()
is used when really some thing went wrong. If you want to exit function use return true/false
. Big applications are saving errors and it will be sad if it will log thousand of errors from correct working code.
I dislike answering things that aren't a real solution...
...but when I encountered this same problem, I made below workaround:
function doThis() {
var err=0
if (cond1) { alert('ret1'); err=1; }
if (cond2) { alert('ret2'); err=1; }
if (cond3) { alert('ret3'); err=1; }
if (err < 1) {
// do the rest (or have it skipped)
}
}
Hope it can be useful for anyone.
If you are using jquery. This should stop the function from bubbling up to so the parent function calling this should stop as well.
function myfunction(e)
{
e.stopImmediatePropagation();
................
}
stopImmediatePropagation()
is not a jQuery thing, and stopping propagation is not the same thing as exiting a function.
Apr 30, 2013 at 15:01
type any random command that throws an error, for example:
exit
or
die:-)
for
loop. Even then, I have no idea why the method suggested would be used, when you could just callbreak;
. To use the example from the article:if(i==5) break;
Usingreturn
will halt the execution of the function, whether or not you're in afor
loop.return
will stop the execution of the function, which seems to be what you asked.