user1956737
user1956737

Reputation: 43

Is there a JavaScript function like PHP‘s exit()?

I need to stop my script. I don't want to write more code like if(...) return false because I will use it many times.

My English is poor. It's my first question on this site. :)

Upvotes: 3

Views: 1298

Answers (2)

Nippey
Nippey

Reputation: 4739

In order to exit ALL scopes of your script, you could throw an exception and catch it at the outer scope of your script ;) This imposes, that you adapt your exception handling.

Please note: It is better to write your program in such way, that the following is NOT necessary!

function ExitException() {}

try { //Do your script-stuff here

  Foo();

  //and do this at any place where you wish to exit:
  throw new ExitException;
}
catch (e) {
  if (e instanceof ExitException) //This is an Exit. All fine.
    console.log("Exit");
  else //Pass Exception to the Browser
    throw e;
}

Upvotes: 0

Fawad Ghafoor
Fawad Ghafoor

Reputation: 6207

you can simply write return false; to do so.

Upvotes: 4

Related Questions