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

JS基础_一元运算符

时间:2017-10-10 23:20:23      阅读:288      评论:0      收藏:0      [点我收藏+]

标签:font   数据类型   type   console   body   tle   基础   ack   number   

 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基础_一元运算符

标签:font   数据类型   type   console   body   tle   基础   ack   number   

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

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