Javascript exit function with return from another function

Javascript exit function with return from another function

javascript stop function from another function
javascript exit function from loop
stop javascript function onclick
return javascript
javascript return not exiting function
else stop function javascript
how to end a function
javascript break nested loop

A better understanding is by code. Let say I have the function bellow:

const firstFct = function() {
    if(!valid) {
        return 'error...';
    }

    return ...
}

I want to exit firstFct using a second function. Instead of using that if .

const secondFct = function(value, msg) {
    if(!value) {
        return msg;
    }
}

const firstFct = function() {
    secondFct(valid, "error"); // this should exit **firstFct** only if !value

    return ...
}

Not sure if there's a way to achieve this with return.


You need to wrap the result of the first call to distinguish a result from an error for an early exit.

const firstFct = function() {
    if (!valid) {
        return { error: 'error...' };
    }
    return ...
}

const firstFct = function() {
    var temp = secondFct(valid, "error");

    if (temp && typeof temp === 'object' && 'error' in temp) {
        return temp.error;
    }
    return ...
}

Or use an exception handling and throw an error.

Early exit from function?, function myfunction() { if(a == 'stop') return; }. This will send a return value Of course, you can specify a different return value. Whatever value is like a common use. Derived this answer: JavaScript equivalent of PHP's die. Sometimes we need to exit the middle of functions in that cases there is a return keyword in JavaScript which helps us to stop the running function. The return keyword exits the function by returning a undefined .


Stop Javascript Function execution from another Function, JavaScript is normally single threaded - meaning that while a function is executed in the browser no other code can run at the same time  Call a JavaScript function with return value. While parameters are used to pass one or more values into a JavaScript function, you can use a return value to pass a value out of the function as well. The function can use the return statement to exit and pass some value back to the caller when it’s completed execution.


Well here a possible solution. Although I would not recommend it.

(I would recommend Nina Scholz Version)

function myFunction() {myFunction:{
    console.log('i get executed');
    break myFunction;
    console.log('i do not get executed');
}}

Now with this pricinple you could do:

const firstFct = function() {myScope: {
    secondFct(valid, "error") ? break myScope: null;
}};

Now if you return a truthy value the execution is suspended, else it continues. I know this is not 100% what you wanted, but it is as good as it gets.

Alternatively you can throw an error, which will suspend the JavaScript Execution. I also do not recommend this.

Generally, we might be able to help you a bit better for a more sophisticated example. There might be another solution for your problem.

How do you break function with return and continue, Once the return statement is reached none of the subsequent code in the function runs - so if the return is inside the end of a block attached to  Those other languages allowed you to exit() or abort() from. a function or procedure. The Javascript "return" statement. will do the same thing. Just put your code in a function. and invoke it, rather than let it run in-line.


return, If the value is omitted, undefined is returned instead. The following return statements all break the function execution: return; return true; return  To return a string from a JavaScript function, use the return statement in JavaScript.ExampleYou need to run the following code to learn how to return a string


Functions :: Eloquent JavaScript, A return statement determines the value the function returns. When control comes In the other case, it returns to the end of the program. The place where the  The return statement stops the execution of a function and returns a value from that function. Read our JavaScript Tutorial to learn all you need to know about functions. Start with the introduction chapter about JavaScript Functions and JavaScript Scope.


JavaScript break Statement, This will stop the execution of more execution of code and/or case testing inside the block. When the break statement is used in a loop, it breaks the loop and  If you want to simply exit a function, you can use "return" to return from the function (optionally with a return value) or "throw" to throw an exception which exits every function until it is caught (if it all) with a try/catch statement.