JS基础_一元运算符

 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>
原文地址:https://www.cnblogs.com/ZHOUVIP/p/7648221.html