Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How does javascript jump out of the function?

Shulou Source: shulou.com Published: 2022-05-31 11:44:56 09月23日 Update

This article mainly introduces the relevant knowledge of javascript how to jump out of the function, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value, I believe you will gain something after reading this javascript how to jump out of the function article, let's take a look at it.

Javascript can jump out of the function and can be implemented using the return keyword. Whenever JavaScript sees the return keyword, it immediately exits the function, and any variables (or values) passed after return are returned as a result.

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

Sometimes when you are executing a function, you want to have a quick exit (pop-out) method.

You can use the return keyword to do this.

Whenever JavaScript sees the return keyword, it immediately exits the function, and any variables (or values) passed after return are returned as a result.

This is a method I often use to ensure that I exit a function immediately when some conditions are different from what I expected.

Maybe I expect a parameter, but it's not there.

Function calculateSomething (param) {if (! param) {return} / / go on with the function}

If the param value exists, the function will proceed as expected, otherwise it will stop immediately.

In this example, I return an object that describes the error.

Function calculateSomething (param) {if (! param) {return {error: true, message: 'Parameter needed'}} / / go on with the function} on "how to jump out of the function" this article is introduced here, thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to jump out of the function of javascript". If you want to learn more knowledge, you are welcome to follow the industry information channel.

Tags: Functions keys keywords knowledge content variables methods articles results values examples parameters objects easy to operate tutorials articles easy to understand more conditions Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Shulou Information NVidia MariaDB MySQL