注:当不清楚运算符的优先级时,给需要优先运算的式子加上()
1.算数运算
算数运算符+,—,*,/,%
1.除+运算外,先将两边的值转为number,再计算
2.+,有一边为字符串,结果一定为字符串
示例:var a = 10;
var b = 20;
console.log(a+b);
console.log(a-b);
console.log(a*b);
console.log(10/20)
2.比较运算
在比较运算中,先将两边的值转为number,再计算Number
返回值是boolean值
示例;var a = 10;
var a = 20;
console.log(a>b);
console.log(a<b);
console.log(a==b);
/* =是赋值语句 ==才是比较运算 */
console.log(a!=b);
3.逻辑运算
&&逻辑与,||逻辑或,!逻辑非
1.逻辑与
特点:只有两边都为true,结果才为true,只要有一边为false,结果就为false
console.log(10>5 && 30>20)
console.log(10<5 && 30>20)
2.逻辑或
特点:只要有一边为true,结果就为true,如果两边都为falase,结果一定是false
console.log(10>20 || 30>20)
3.逻辑非
console.log(!(30>40))
4.赋值运算
赋值运算 +=,-=,*=,/=,%
var a = 10;
a+=20;
// a = a+20;
console.log(a);
var b = 30;
b-=40;
// b=b-40
console.log(b);
5.三元(三目)
语法:条件表达式?语句1:语句二
经典三木运算:
比大小:
var a =30;
var b =140;
var c = 50;
var max = a>b?(a>c?a:c):(b>c?b:c)
/* var value = (code)? block1:block2;
code == true 输出block1
code == false 输出block2
*/
var a = (10<5)?"true":"false";
console.log(a)
6.++
++放在后面,先运算后自增
++放在前面,先自增后运算
// var b = ++a +20;
// console.log(a); //21
// console.log(b); //41
var b = a++ +20;
console.log(a); //21
console.log(b); //40