How can I exit from a javascript function? [duplicate]

Samantha J T Star提出了一个问题:How can I exit from a javascript function? [duplicate],或许与您遇到的问题类似。

回答者Florian Margaine给出了该问题的处理方式:

if ( condition ) {
    return;
}

The return exits the function returning undefined.

The exit statement doesn't exist in javascript.

The break statement allows you to exit a loop, not a function. For example:

var i = 0;
while ( i < 10 ) {
    i++;
    if ( i === 5 ) {
        break;
    }
}

This also works with the for and the switch loops.

希望本文对你有帮助,欢迎支持JavaScript中文网