JavaScript return
return 语句用于将特定值从函数返回到函数调用者。调用
return 语句时,该函数将停止执行。
return 语句应该是函数中的最后一条语句,因为
return 语句之后的代码将无法访问。
我们可以返回原始值(例如通过使用
return 语句
我们可以返回多个值,例如布尔值,数字,字符串等)和对象类型(例如函数,对象,数组等)。使用
return 语句的值。无法直接完成。我们必须使用
Array 或
Object 从函数返回多个值。
语法
上述语法中的
expression 是返回给函数调用者的值。它是可选的。如果未指定
expression ,则该函数返回
undefined 。
不允许在之间使用行终止符
return关键字和值。我们可以通过以下几行来理解它。假设我们正在编写
return 语句,如下所示:
然后,它将被转换为-
在
return 语句之后会自动插入分号。在
return 语句(
x + y; )之后编写的代码将被视为
无法访问的代码。
我们可以使用括号来防止此问题。可以写成-
示例1
这是使用
return 语句的简单示例。在这里,我们返回两个数的乘积结果,并将值返回给函数调用者。
变量
res 是函数调用者;它正在调用函数
fun()并传递两个整数作为函数的参数。结果将存储在
res 变量中。在输出中,值
360 是参数
12 和
30 的乘积。
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<p> Welcome to the lidihuo.com </p>
<p> Example of the JavaScript's return statement </p>
<script>
var res = fun(12, 30);
function fun(x, y)
{
return x * y;
}
document.write(res);
</script>
</body>
</html>
输出
Welcome to the lidihuo.com
Example of the JavaScript's return statement
Example2
在这里,我们正在使用
return 语句中断函数。调用
return 语句时,该函数立即停止执行。
循环中有无限的
while 和变量
i,初始化为1。循环继续进行,直到
i 的值达到
4 。当变量的值为4时,由于
return 语句,循环将停止执行。循环之后的语句将永远不会执行。
在这里,
return 语句无需使用
expression ,因此它返回
undefined。
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<p> Welcome to the lidihuo.com </p>
<p> Example of the JavaScript's return statement </p>
<script>
var x = fun();
function fun() {
var i = 1;
while(i) {
document.write(i + '<br>');
if (i == 4) {
return;
}
document.write(i + '<br>');
i++;
}
document.write('Hello world');
}
</script>
</body>
</html>
输出
Welcome to the lidihuo.com
Example of the JavaScript's return statement
现在,我们将看到如何使用
return 语句返回多个值。通常,JavaScript函数返回一个值,但是我们可以使用
array 或
object 返回多个值。要返回多个值,我们可以将这些值打包为对象的属性或数组元素。
示例3-使用数组返回多个值
在此示例中,使用
Array 返回多个值。在这里,我们使用
ES6数组解构语法来解压缩数组的值。
<!DOCTYPE html>
<html>
<head>
<title> JavaScript return </title>
</head>
<body>
<p> Welcome to the lidihuo.com </p>
<p> This is an example of returning multiple values using array </p>
<script>
function getData() {
let fname = 'John',
lname = 'Rickman',
age = '25',
occupation = 'Private Employee';
return [fname, lname, age, occupation];
}
const [fname, lname, age, occupation] = getData();
document.write("Name = " + fname + " " + lname + "<br>");
document.write("Age = " + age + "<br>");
document.write("Occupation = " + occupation);
</script>
</body>
</html>
输出
Welcome to the lidihuo.com
This is an example of returning multiple values using array
Example4-使用 object
返回多个值在此示例中,我们通过使用
Object 返回多个值。在这里,我们使用
ES6对象解构语法来解压缩对象的值。
<!DOCTYPE html>
<html>
<head>
<title> JavaScript return </title>
</head>
<body>
<p> Welcome to the lidihuo.com </p>
<p> This is an example of returning multiple values using object </p>
<script>
function getData() {
let fname = 'John',
lname = 'Rickman',
age = '25',
occupation = 'Private Employee';
return {
fname,
lname,
age,
occupation
};
}
let {fname, lname, age, occupation} = getData();
document.write("Name = " + fname + " " + lname + "<br>");
document.write("Age = " + age + "<br>");
document.write("Occupation = " + occupation);
</script>
</body>
</html>
输出
Welcome to the lidihuo.com
This is an example of returning multiple values using object