首页 > Web开发 > 详细

JS基础_一元运算符

时间:2017-10-10 23:01:02      阅读:295      评论:0      收藏:0      [点我收藏+]
 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              *     - 负号
13              *         - 负号可以对数字进行负号的取反
14              * 
15              *     - 对于非Number类型的值,它会将先转换为Number,然后再运算
16              *         
17              *     + 可以对一个其他的数据类型使用+,来将其转换为number,它的原理和Number()函数一样
18              */
19             
20             var a = 123;
21             
22             a = -a;
23             console.log(a); //-123
24             console.log(typeof a); //number
25             
26             
27             //对于非Number类型的值,它会将先转换为Number,然后再运算
28             b = true;  
29             b = -b;
30             console.log(b); //-1
31             
32             
33             //可以对一个其他的数据类型使用+,来将其转换为number,它的原理和Number()函数一样
34             a = "18";
35             a = +a;
36             console.log(a); //18
37             console.log(typeof a); //number
38             
39             
40             //可以对一个其他的数据类型使用+,来将其转换为number,它的原理和Number()函数一样
41             var result = 1 + +"2" + 3;
42             console.log(result); //6
43             
44             
45             
46         </script>
47     </head>
48     <body>
49     </body>
50 </html>

 

JS基础_一元运算符

原文:http://www.cnblogs.com/ZHOUVIP/p/7648221.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!