Home > Web Front-end > JS Tutorial > body text

Detailed explanation of how to terminate function operation code in javascript

伊谢尔伦
Release: 2017-07-25 14:40:12
Original
2351 people have browsed it

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

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

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

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

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');
}
Copy after login

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();
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!