What I do is set up a variable at the very start to shut down things and then have that variable checked to either run the script to stop. so something like this:
var exitProg = false;
if(some condition met){runProg}
else{exitProg = true}
function runProg{
Your code here...
if(another condition checked){
secondFunction()
More code}
else{
exitProg = true;
return}
}
function secondFunction(){
More code here.....
}
By setting the exitProg= true, you can then write your program to skip over everything else and just finish without running any more code.