javascript的运算符

  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              *     比如:typeof就是运算符,可以来获得一个值的类型
 12              *         它会将该值的类型以字符串的形式返回
 13              *         number string boolean undefined object
 14              * 
 15              *     算数运算符
 16              *         当对非Number类型的值进行运算时,会将这些值转换为Number然后在运算
 17              *             任何值和NaN做运算都得NaN
 18              * 
 19              *         +
 20              *             +可以对两个值进行加法运算,并将结果返回
 21              *              如果对两个字符串进行加法运算,则会做拼串
 22              *                 会将两个字符串拼接为一个字符串,并返回
 23              *             任何的值和字符串做加法运算,都会先转换为字符串,然后再和字符串做拼串的操作
 24              *         -
 25              *             - 可以对两个值进行减法运算,并将结果返回
 26              * 
 27              *         *
 28              *             * 可以对两个值进行乘法运算
 29              *         /
 30              *             / 可以对两个值进行除法运算
 31              *         %
 32              *             % 取模运算(取余数)
 33              */
 34             var a = 123;
 35             
 36             var result = typeof a;
 37             
 38             //console.log(typeof result);
 39             
 40             result = a + 1;
 41             
 42             result = 456 + 789;
 43             
 44             result = true + 1;
 45             
 46             result = true + false;
 47             
 48             result = 2 + null;
 49             
 50             result = 2 + NaN;
 51             
 52             result = "你好" + "大帅哥";
 53             
 54             var str = "锄禾日当午," +
 55                       "汗滴禾下土," +
 56                       "谁知盘中餐," +
 57                       "粒粒皆辛苦";
 58                       
 59                       
 60             result = 123 + "1";
 61             
 62             result = true + "hello";
 63             
 64             //任何值和字符串相加都会转换为字符串,并做拼串操作
 65             /*
 66              * 我们可以利用这一特点,来将一个任意的数据类型转换为String
 67              *     我们只需要为任意的数据类型 + 一个 "" 即可将其转换为String
 68              *     这是一种隐式的类型转换,由浏览器自动完成,实际上它也是调用String()函数
 69              */
 70             var c = 123;
 71             
 72             c = c + "";
 73             
 74             //c = null;
 75             
 76             //c = c + "";
 77             
 78             
 79             //console.log(result);
 80             //console.log(typeof c);
 81             //console.log("c = "+c);
 82             
 83             result = 1 + 2 + "3"; //33
 84             
 85             result = "1" + 2 + 3; //123
 86             
 87             result = 100 - 5;
 88             
 89             result = 100 - true;
 90             
 91             result = 100 - "1";
 92             
 93             result = 2 * 2;
 94             
 95             result = 2 * "8";
 96             
 97             result = 2 * undefined;
 98             
 99             result = 2 * null;
100             
101             result = 4 / 2;
102             
103             result = 3 / 2;
104             
105             /*
106              * 任何值做- * /运算时都会自动转换为Number
107              *     我们可以利用这一特点做隐式的类型转换
108              *         可以通过为一个值 -0 *1 /1来将其转换为Number
109              *         原理和Number()函数一样,使用起来更加简单
110              */
111             
112             var d = "123";
113             
114             //console.log("result = "+result);
115             
116             d = d - 0;
117             
118             /*console.log(typeof d);
119             console.log(d);*/
120             
121             result = 9 % 3;
122             result = 9 % 4;
123             result = 9 % 5;
124             
125             console.log("result = "+result);
126             
127         </script>
128     </head>
129     <body>
130     </body>
131 </html>

<!DOCTYPE html><html><head><meta charset="UTF-8"><title></title><script type="text/javascript">/* * 运算符也叫操作符 *通过运算符可以对一个或多个值进行运算,并获取运算结果 *比如:typeof就是运算符,可以来获得一个值的类型 *它会将该值的类型以字符串的形式返回 *number string boolean undefined object *  *算数运算符 *当对非Number类型的值进行运算时,会将这些值转换为Number然后在运算 *任何值和NaN做运算都得NaN *  *+ *+可以对两个值进行加法运算,并将结果返回 * 如果对两个字符串进行加法运算,则会做拼串 *会将两个字符串拼接为一个字符串,并返回 *任何的值和字符串做加法运算,都会先转换为字符串,然后再和字符串做拼串的操作 *- *- 可以对两个值进行减法运算,并将结果返回 *  ** ** 可以对两个值进行乘法运算 */ */ 可以对两个值进行除法运算 *% *% 取模运算(取余数) */var a = 123;var result = typeof a;//console.log(typeof result);result = a + 1;result = 456 + 789;result = true + 1;result = true + false;result = 2 + null;result = 2 + NaN;result = "你好" + "大帅哥";var str = "锄禾日当午," +  "汗滴禾下土," +  "谁知盘中餐," +  "粒粒皆辛苦";    result = 123 + "1";result = true + "hello";//任何值和字符串相加都会转换为字符串,并做拼串操作/* * 我们可以利用这一特点,来将一个任意的数据类型转换为String *我们只需要为任意的数据类型 + 一个 "" 即可将其转换为String *这是一种隐式的类型转换,由浏览器自动完成,实际上它也是调用String()函数 */var c = 123;c = c + "";//c = null;//c = c + "";//console.log(result);//console.log(typeof c);//console.log("c = "+c);result = 1 + 2 + "3"; //33result = "1" + 2 + 3; //123result = 100 - 5;result = 100 - true;result = 100 - "1";result = 2 * 2;result = 2 * "8";result = 2 * undefined;result = 2 * null;result = 4 / 2;result = 3 / 2;/* * 任何值做- * /运算时都会自动转换为Number *我们可以利用这一特点做隐式的类型转换 *可以通过为一个值 -0 *1 /1来将其转换为Number *原理和Number()函数一样,使用起来更加简单 */var d = "123";//console.log("result = "+result);d = d - 0;/*console.log(typeof d);console.log(d);*/result = 9 % 3;result = 9 % 4;result = 9 % 5;console.log("result = "+result);</script></head><body></body></html>

猜你喜欢

转载自www.cnblogs.com/lvjianqun/p/10300891.html