Stopping a JavaScript function when a certain condition is met

前端 未结 6 1926
无人共我
无人共我 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:16

    Return is how you exit out of a function body. You are using the correct approach.

    I suppose, depending on how your application is structured, you could also use throw. That would typically require that your calls to your function are wrapped in a try / catch block.

提交回复
热议问题