views:

80

answers:

3

I have a function:

    function myfunction()
    {
         if (a == 'stop')  // How can I stop the function here?
         ................
    }

Is there something like exit() in javascript?

Thanks.

+5  A: 

Replace the .......... with return;

jeffamaphone
+3  A: 

You can just use return.

function myfunction() {
     if(a == 'stop') 
         return;
}

This will send a return value of undefined to whatever called the function.

var x = myfunction();

console.log( x );  // console shows undefined

Of course, you can specify a different return value. Whatever value is returned will be logged to the console using the above example.

return false;
return true;
return "some string";
return 12345;
patrick dw
A: 

This:

function myfunction()
{
     if (a == 'stop')  // How can I stop working of function here?
     {
         return;
     }
}
Rob