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>