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

Tips on using return/break statements (code examples)

不言
Release: 2019-03-18 11:46:19
forward
2354 people have browsed it

This article brings you the usage skills (code examples) of return/break statements. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. helped.

In JavaScript, generally when we want the program to be executed halfway through the function, we will stop executing the following code after judgment or completing a certain operation, and we will use return to end the function.

For example:

//去除前后的空格
function spaceWord(vRet){
if(!vRet) return vRet;
//条件循环 当不满足自身的条件的时候继续循环,当达到条件的时候 终止运行 一般用return 
//while这样的循环语句可以中断代码的执行,
while (true) {//推出条件
    if (vRet.indexOf (' ') == 0) {
        vRet = vRet.substring(1, parseInt(vRet.length));
    } else if ((parseInt(vRet.length) != 0) && (vRet.lastIndexOf (' ') == parseInt(vRet.length) - 1)) {
        vRet = vRet.substring(0, parseInt(vRet.length) - 1);
    } else {
        return vRet;//使用return 终止运行 跳出执行体
    }
}
}
console.log(spaceWord(spaceStr));
Copy after login

But, what if I want to make multiple layers of judgments, and only want to interrupt one of the judgmentsThe code behind it?

function(){
//代码1...
if(stopRun == true){
    //代码2 ...
    if(cando == false){
        return
    }
    //代码3 ...
}
//代码4 ...
}
Copy after login

If return is used to terminate the operation, code 3 and code 4 cannot be executed;

For this requirement, use break to interrupt the code without ending the entire function.

For example:

var stopRun = true,cando = false;
function ww(){
//代码1...
while(stopRun == true){
    //代码2 ...
    console.log(2)
    if(cando == false){
        break
    }
    //代码3 ...
    console.log(3)
    break
}
//代码4 ...
console.log(4)
}
Copy after login

The above logic, when executing the code 2 statement and encountering the break statement, the code jumps out of the loop and executes code 4. The running output is: 2 4; This proves that code 3 is blocked, but does not prevent the function from running;

break cannot be used in if, but it can be used in loops, so using loop statements such as while can interrupt the execution of the code. In theory All loops can implement this function, and while is chosen because it is simple and can replace if for judgment.
!!!Attention!!!Remember to add a break at the end of while, otherwise it will become an infinite loop.

You can add multiple if()breaks as breakpoints in the middle of while to control the flow of logic.

function(){
var a=0;
while(a==0){
    console.log("我要")
    a++
    if(a!=1)break
    console.log("我再要")
    a++
    if(a!=2)break
    console.log("我还要")
    break
}
}
Copy after login

The above is the detailed content of Tips on using return/break statements (code examples). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
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!