Stopping a JavaScript function when a certain condition is met

前端 未结 6 1937
无人共我
无人共我 2020-12-23 19:04

I can\'t find a recommended way to stop a function part way when a given condition is met. Should I use something like exit or break?

I am

6条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-23 19:23

    The return statement exits a function from anywhere within the function:

    function something(x)
    {
        if (x >= 10)
            // this leaves the function if x is at least 10.
            return;
    
        // this message displays only if x is less than 10.
        alert ("x is less than 10!");
    }
    

提交回复
热议问题