How to conclude a JavaScript App

Discussion in 'JavaScript' started by mcfc4eva, Mar 29, 2009.

  1. #1
    I have a JavaScript application on my site such as;

    if(CONDITION){
    [COLOR="Red"]CONCLUDE SCRIPT[/COLOR]
    }
    else if{
    EXECUTE THIS
    }
    [COLOR="RoyalBlue"]MORE CODE HERE[/COLOR]
    Code (markup):
    I want to make the script finish if the CONDITION is true. So no code will be executed after the CONCLUDE SCRIPT such as the MORE CODE HERE.

    Is there such an option in JavaScript?
     
    mcfc4eva, Mar 29, 2009 IP
  2. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #2
    nest it within a function and then use return; to exit the function:
    
        <script type="text/javascript" >
        new function main(){
            var a = 1;
    
            if(a==1)
                return;
            else
                alert(a);
    
    	//execute more code
            alert(a + 8 - 2);
        }
        </script>
    
    Code (markup):
    Using 'new function main() {' will call the function when it is created.
     
    camjohnson95, Mar 29, 2009 IP
  3. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #3
    you can return whatever value if necessary
     
    camjohnson95, Mar 29, 2009 IP
  4. mcfc4eva

    mcfc4eva Well-Known Member

    Messages:
    602
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    110
    #4
    thanks, this really helped! :)
     
    mcfc4eva, Mar 30, 2009 IP
  5. mji2010

    mji2010 Active Member

    Messages:
    762
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    55
    #5
    you can also use the break statement

    if(condition){
    break;
    }else{
    statements...
    }
     
    mji2010, Apr 1, 2009 IP