/*
* 运算符也叫操作符
* 通过运算符可以对一个或多个值进行运算
* 比如:typeof 就是运算符,可以来获得一个值的类型
*
* 算数运算符
* 当对非Number类型的值进行运算时,会先转换成数字进行运算
* 任何值和NaN进行运算都是NaN
* +
* +可以对两个值进行加法运算,并将结果返回
* 不会对原来值改变
* 当对两个字符串进行加法操作时会进行拼串操作
* 任何的值和字符串做加法运算,都会先转换成字符串再进行拼串操作
* 我们可以利用这一特点,来将一个任意的数据类型转换为String
* 我们只要为任意的数据类型+一个""即可将其转换为String
* 这种时一种隐式的类型转换,由浏览器自动完成,其实实际上也是调用String()函数
* -
* *
* /
* %
* %其模就是两个数做商取余数
*/
var a = 123;
var result = typeof a;
a = a +1
console.log(a);
result = 456+89;
result = true + 1;
result = true+false;
result = 2+null;
result = 2+NaN;
result = "123"+"456"
result1 = 123+"1"
result2 = true + "hello";
var str = "锄禾日当午,"+
"汗滴禾下土,"+
"谁知盘中餐,"+
"粒粒皆辛苦。"
console.log(result);
console.log(str);
console.log(result1);
console.log(typeof result1);
console.log(result2);
var c = 123;
c = c+"";
console.log(typeof c);
console.log(c)
console.log("c = "+c);
console.log(typeof result);
result = 1+2+"3";//33
result = "1"+2+3;//123
console.log("result = "+result);
result1 = 100-5;
result2 = 100-true;
result3 = 100- "1";
console.log("result1 = "+result1);
console.log("result2 = "+result2);
console.log("result3 = "+result3);
result = 9 %4
console.log("result = "+result);
/*
* 任何值做- * /运算时都会自动转换为Number
* 我们可以利用这一特点做隐式的类型转换
* 可以通过为一个值-0 *1 /1 来将其转换为Number
*
*/
var d = "123";
d = d -0;
console.log(typeof d);
console.log(d);
操作控制台的输出结果:
[Web浏览器] “124” /初级教程02/08运算符.html (34)
[Web浏览器] “123456” /初级教程02/08运算符.html (47)
[Web浏览器] “锄禾日当午,汗滴禾下土,谁知盘中餐,粒粒皆辛苦。” /初级教程02/08运算符.html (48)
[Web浏览器] “1231” /初级教程02/08运算符.html (49)
[Web浏览器] “string” /初级教程02/08运算符.html (50)
[Web浏览器] “truehello” /初级教程02/08运算符.html (51)
[Web浏览器] “string” /初级教程02/08运算符.html (54)
[Web浏览器] “123” /初级教程02/08运算符.html (55)
[Web浏览器] “c = 123” /初级教程02/08运算符.html (56)
[Web浏览器] “string” /初级教程02/08运算符.html (58)
[Web浏览器] “result = 123” /初级教程02/08运算符.html (62)
[Web浏览器] “result1 = 95” /初级教程02/08运算符.html (68)
[Web浏览器] “result2 = 99” /初级教程02/08运算符.html (69)
[Web浏览器] “result3 = 99” /初级教程02/08运算符.html (70)
[Web浏览器] “result = 1” /初级教程02/08运算符.html (73)
[Web浏览器] “number” /初级教程02/08运算符.html (84)
[Web浏览器] “123” /初级教程02/08运算符.html (85)