Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP's exit; in Javascript?

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...

like image 744
Jamesking56 Avatar asked Sep 04 '12 10:09

Jamesking56


People also ask

How do I exit a PHP script?

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.

How do you give exit in JavaScript?

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.

What does exit 0 do in PHP?

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.

Does return exit the function PHP?

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.


2 Answers

You could try:

throw "stop execution";

Using return you will skip the current function, that's why throwing is more similar to PHP exit();

like image 191
arutaku Avatar answered Oct 13 '22 12:10

arutaku


I would finish a function early with a return statement:

return;
like image 25
Florian Bauer Avatar answered Oct 13 '22 12:10

Florian Bauer