码迷,mamicode.com
首页 > Web开发 > 详细

JS基础_函数的返回值

时间:2017-10-17 23:00:52      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:使用   html   function   body   返回   log   cti   console   hello   

 1 <!DOCTYPE html>
 2 <html>
 3     <head>
 4         <meta charset="utf-8" />
 5         <title></title>
 6         <script type="text/javascript">
 7             
 8             
 9             /*
10              * 创建一个函数,用来计算三个数的和
11              * 
12              * 可以使用 return 来设置函数的返回值
13              *     语法:
14              *         return 值
15              * 
16              *     return后的值将会会作为函数的执行结果返回,
17              *         可以定义一个变量,来接收该结果
18              * 
19              *  在函数中return后的语句都不会执行
20              * 
21              *     如果return语句后不跟任何值就相当于返回一个undefined,
22              *     如果函数中不写return,则也会返回undefined
23              * 
24              *     return后可以跟任意类型的值
25              * 
26              */
27             
28             function sum(a , b , c){
29                 
30                 var d = a + b + c;
31                 
32                 return d;
33                 
34                 //return undefined;
35                 
36             }
37             
38             //调用函数
39             //变量result的值就是函数的执行结果
40             //函数返回什么result的值就是什么
41             var result = sum(4,7,8);
42             console.log("result = "+result);//19
43             
44             
45             var result2 = alert("hello");
46             console.log("result2 = "+result2);//undefined,说明alert()是没有返回值的
47             
48             
49             
50         </script>
51     </head>
52     <body>
53         
54     </body>
55 </html>

 

JS基础_函数的返回值

标签:使用   html   function   body   返回   log   cti   console   hello   

原文地址:http://www.cnblogs.com/ZHOUVIP/p/7684152.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!