JavaScript 函数是否必须返回一个值?
一个 JavaScript 函数可以具有一个可选的 return 语句,即返回一个值是可选的。此语句应该是函数中的最后一条语句。
例如,你可以在一个函数中传入两个数字,然后你可以期望该函数将它们的乘积返回给你的调用程序。
示例
尝试一下以下示例。它定义了一个函数,该函数采用两个参数,并将它们连接起来,然后再在调用程序中返回结果。
<html>
<head>
<script type = "text/javascript">
function concatenate(first, last){
var full;
full = first + last;
return full;
}
function secondFunction(){
var result;
result = concatenate('John', 'Doe');
document.write (result );
}
</script>
</head>
<body>
<p>Click the following button to call the function</p>
<form>
<input type = "button" onclick = "secondFunction()" value = "Call Function">
</form>
<p>Use different parameters inside the function and then try...</p>
</body>
</html>
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP