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