What is the equivalent of PHP's exit; in Javascript/jQuery?
I need to stop my script early depending on certain conditions... The only answers I could find from search was stopping forms from submitting...
The exit() function in PHP is an inbuilt function which is used to output a message and terminate the current script. The exit() function only terminates the execution of the script. The shutdown functions and object destructors will always be executed even if exit() function is called.
Using return to exit a function in javascript Using return is the easiest way to exit a function. You can use return by itself or even return a value.
Exit statuses should be in the range 0 to 254, the exit status 255 is reserved by PHP and shall not be used. The status 0 is used to terminate the program successfully.
The return keyword ends a function and, optionally, uses the result of an expression as the return value of the function. If return is used outside of a function, it stops PHP code in the file from running.
You could try:
throw "stop execution";
Using return you will skip the current function, that's why throwing is more similar to PHP exit();
I would finish a function early with a return statement:
return;
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