Home  >  Article  >  Web Front-end  >  Detailed explanation of how to terminate function operation code in javascript

Detailed explanation of how to terminate function operation code in javascript

伊谢尔伦
伊谢尔伦Original
2017-07-25 14:40:122254browse

1. If you want to terminate a function, just use return. The example is as follows:

function testA(){
    alert('a');
    alert('b');
    alert('c');
}

testA(); When the program is executed, 'a', 'b', 'c will pop up in sequence. '.

function testA(){
    alert('a');
    
return;
    alert('b');
    alert('c');
}

testA(); Program execution will terminate when 'a' pops up.

2. When calling other functions in a function, when the called function is terminated, the calling function is also expected to be terminated. The example is as follows:

function testC(){
    alert('c');
    return;
    alert('cc');
}

function testD(){
    testC();
    alert('d');
}

We see that in TestC is called in testD. In testC, I want to terminate testD through return. However, contrary to expectations, return only terminates testC. When the program is executed, 'c' and 'd' will pop up in sequence.

function testC(){
    alert('c');
    
return false;
    alert('cc');
}

function testD(){
    if(!testC()) return;
    alert('d');
}
testD();

The two functions have been modified. TestC returns false, and testD judges the return value of testC. In this way, when testC is terminated, testD can also be terminated. The program execution will terminate when 'c' pops up. .

The above is the detailed content of Detailed explanation of how to terminate function operation code in javascript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn