How to stop function execution in javascript

青灯夜游
Release: 2023-01-04 09:36:09
Original
13922 people have browsed it

In JavaScript, you can use the "return" keyword to stop function execution; just add the "return;" code to the function. Return literally means return. In the official definition, the return statement is used to terminate the execution of the current function and return the value of the current function.

How to stop function execution in javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

In javascript, 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 will pop up in sequence. ','b','c'.

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

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

[Recommended learning:javascript advanced tutorial]

javascript return statement

JavaScript return statement, indicating from The called function returns to the calling function to continue execution. The return can be accompanied by a return value, which is specified by the parameter after return. Return is usually necessary because the calculation result is usually brought out through the return value when the function is called. If the function execution does not need to return calculation results, it is often necessary to return a status code to indicate whether the function is executed smoothly or not (-1 and 0 are the most commonly used status codes). The calling function can judge the execution of the called function through the return value. Condition.

The function of return value is to encapsulate fixed and commonly used code and write it in a class. Just call it every time it is used.

First let’s briefly summarize the actual use of renturn.

1. The return statement terminates the execution of the function and returns the value of the function.

Write a button to trigger the onclick event

function demo1() { return1; document.write(' '); }
Copy after login

When returning, the function value is equal to 1, subsequent code will not be executed, and there will be no subsequent output.

function demo2() { function a(b,c) { return b+c; document.write(''); } var abc=a(5,80); alert(abc); }
Copy after login

When calling the variable abc, its value will be 85; if you want to output the result directly without returning a value, delete return b c. In this way, abc will have no value, but the web page will output 85.

It can also be said that the default function has no return value. Usually the function needs to return a value or object to the outside after a series of processing. For example,

function demo3() { function sum(a,b){ returna+b } alert(sum(1,2)); }
Copy after login

At this time, the value displayed by alert is 3, and if return is removed, undefined will be returned.

2. Application based on function return value

The application of return when terminating the function: return return: false; for the event processing function, which is used to prevent the default event behavior and cancel the default action. For example,

demo4
Copy after login

Add a return value to it

function demo4() {return false;}
Copy after login

So that clicking on the link will not jump

The above is the common usage of return. At this time, there is a more common problem , that is: if return is used in a for loop, will the next loop be executed?

function demo5() { for(var i=1; ;i++) { console.log(i+"A"); return; console.log(i+"B");} console.log(i+"C");}
Copy after login

Due to the return, the function directly terminates the operation and returns the current value, so the output value of this function is only 1A.

So, for the following function,

function demo6() { function counter() { for(var count=1; ;count++) { console.log(count+"A"); if(count===5) {return;} console.log(count+"B");} console.log(count+"C");} counter();}
Copy after login

the output values are 1A, 1B, 2A, 2B, 3A, 3B, 4A, 4B, 5C.

For more programming related knowledge, please visit:Programming Video! !

The above is the detailed content of How to stop function execution 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
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!