In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you whether the JavaScript function has a return value, I hope you will gain something after reading this article, let's discuss it together!
The JavaScript function has a return value. If the function contains a return statement, once the return statement is executed, it stops the function and calculates and returns the value of the expression after the return; if the function does not contain the return statement, the undefined value is returned after each statement in the body of the function is executed.
The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.
The JavaScript function has a return value.
The JavaScript function provides two interfaces to realize the interaction with the outside world, in which the parameter is used as the entrance to receive the external information, and the return value is used as the exit to feedback the operation result to the outside world.
Inside the function body, you can use the return statement to set the return value of the function. Once the return statement is executed, the function is stopped and the value of the expression following return is evaluated and returned. If the function does not contain a return statement, the undefined value is returned after each statement in the body of the function is executed.
JavaScript is a weakly typed language, so functions have no type restrictions on the values received and output, and JavaScript does not automatically detect the types of input and output values.
Example 1
The following code defines the return value of a function as a function.
Function getSum (num1, num2) {return num1 + num2;} var sum1 = getSum (7,12); / / function return value: 19var sum2 = getSum (- 5,33); / / function return value: 28
Tip: the return statement is usually defined at the end of the function. When the function runs to the return statement, it stops running immediately and returns to the place where the function was called to continue execution.
Example 2
There is no limit to the parameters of the function, but the return value can only be one; if you want to output multiple values, you can design it through an array or object.
Function division (dividend, divisor) {var quotient = dividend / divisor; var arr = [dividend, divisor, quotient] return arr;} var res = division (100,4) [xss_clean] (res [0]); / / output: 100 [xss_clean] (res [1]); / / output: 4 [xss_clean] (res [2]); / / output: 25
In the above code, the function returns an array of three elements, thus returning multiple worthwhile purposes using a return statement.
Example 3
There can be multiple return statements in the function body, but only one return statement can be executed, so you can use the branch structure to determine the function return value in the function body, or use the return statement to terminate the function in advance.
Function f (typeof y) {/ / if the argument is of a non-numeric type, the function terminates the execution of if (typeof x! = "number" | | typeof y! = "number") return; / / returns the conditional value if (x > y) return x-y; if (x < y) return y-x; if (x * y)
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.