算术运算符( + - * / % )
两边都先转换为number,再运算
// + - * / %var a =3;var b = 3.3;var c = 4;console.log(a*b) // 9.899999999999999console.log(b/a) // 1.0999999999999999console.log(c%a) // 1
比较运算符( >,<,==,>=,<=,!= )
两边都先转换为number,再比较,返回的是 boolean
var a = truevar b = 1;console.log(a==b) // trueconsole.log(a=="1") //true 两边都先转为number
特殊情况
==运算中
1.只要有一边为boolean,先将两边转为number
2.如果一边为string,一边为number,先将两边转为number
3.null == undefined
4.null和undefined不能转化成其他值
console.log(false == "");//trueconsole.log(true == 1); //trueconsole.log(null == undefined);//trueconsole.log(undefined == false);//falseconsole.log(1 == "1");//true
逻辑运算符( &&,||,! )
两边都先转换为boolean,再判断,返回的是 boolean值
// && 逻辑与 两个为true,结果才为true// || 或 只要有一个为true,结果就为true// ! 非console.log(3>2 && 4>3) // true && true 结果为trueconsole.log(3>2 && 4<3) // true && false 结果为falseconsole.log(11>5 || 3<4) // true || false 结果为trueconsole.log(!(3>4)) //!false 结果为true
或运算的注意点
|| 有一边为true,结果就为 true,遇到true就会返回
var b = 0 || "abc"var c = "abc" || 1;var d = 0 || NaNconsole.log(b) // abcconsole.log(c) // abcconsole.log(d) // NaN
与运算的注意点
**两边都为true,结果才为true 遇到false的情况,直接返回**
console.log("abc" && 10) // 10console.log(0 && "abc") // 0
三元运算符
// true 输出问号后面的第一段语句// false 输出问号后面的第二段语句var a = (4>5)?"4大于5":"4小于5";console.log(a)
赋值运算(+=,-=)
// +=,-=var a = 2;//a=a+4;a+=4;var b = 4;b-=4; // b=b-4;console.log(a) // 6console.log(b) // 0
