Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize the return value of function by javascript

Shulou Source: shulou.com Published: 2022-06-03 04:10:33 10月27日 Update

Editor to share with you how to achieve the function return value of javascript, I hope you have something to gain after reading this article, let's discuss it together!

In javascript, the statement that implements the return value of the function is return, with the syntax "return [return value];". Inside the function body, the return value of the function can be set using the return statement; once the return statement is executed, the function is stopped and the value of the expression following return is evaluated and returned.

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

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 f () {return function (xrecoy) {return x + y;}}

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 f () {var a = []; a [0] = true; a [1] = function (xQuery y) {return x + y;} a [2] = 123; return a; / / return multiple values}

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)

Tags: Function statement type parameter outside multiple array example output operation run code article expression limit two information element entry branch Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology MySQL Xiaomi Shulou Information vpn