Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use return in JavaScript

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

In this article, the editor introduces in detail "how to use return in JavaScript". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to use return in JavaScript" can help you solve your doubts.

The use of return in JavaScript: 1, return function values, you can return values of any type, including basic data types, objects, functions, etc.; 2, use return statements without return values to abort the execution of the function at any time.

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

The JavaScript return statement does two things in the function definition:

One is to return the function value.

The second is to abort the execution of the function.

Return statements can return values of any type, including basic data types, objects, functions, and so on. Each function returns a value. When no return statement is used, or when return is used, but no return value is indicated after it, the function returns a "undefined" value. If you need to return a value other than "undefined", you must use return and indicate the returned value.

Once the function has finished executing the return statement, it will immediately return the value of the function and abort the execution of the function. At this time, the code after the return statement will not be executed. According to this characteristic of the return statement, when you need to exit the execution of the function ahead of time, the return statement without return value is often used to abort the execution of the function at any time.

[example 1] the return statement explicitly returns the function value.

The return statement explicitly returns the function value function expressionCaculate (x) {if ((x > =-10) & & (x b) {console.log ("an is greater than b"); return; console.log ("a greater than b");} console.log ("axib =" + (aqb));} add (7)

The add () method is called when the add (7P3) code is executed, where the value of the first parameter is greater than the second parameter, the output "an is greater than b" in the console, and then the function returns and stops execution, so that neither of the two logs following the return statement is output. The running result is shown in figure 1.

Figure 1:return statement aborts the result of function execution

[example 3] return statement returns a function.

The return statement returns the function function outerFunc () {var b = 0; return function () {/ / returns the anonymous function breadwinner; console.log ("internal function b =" + b);}} var func = outerFunc (); func ()

Because the outerFunc () function returns an anonymous function, the call expression of the outerFunc function becomes a function expression, so you can use the variable func to call the anonymous function. The running result is shown in figure 2.

Figure 2: calling the anonymous function returned by the return statement

After reading this, the article "how to use return in JavaScript" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report