优先级 | 运算符 | 顺序 |
1 | 小括号 | () |
2 | 一元运算符 | ++ -- ! |
3 | 算数运算符 | 先* / %后+ - |
4 | 关系运算符 | > >= < <= |
5 | 相等运算符 | == !== === !=== |
6 | 逻辑运算符 | 先&&后|| |
7 | 赋值运算符 | = |
8 | 逗号运算符 | , |
- 一元运算符里面的逻辑非优先级很高
- 逻辑与比逻辑或者优先级高
例如:
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>运算符优先级</title><script>var a = 3 > 5 && 2 < 7 && 3 ==4// 声明一个变量,并取名字 3>5则为假 与 2<7则为真 后面直接把用看啦直接为false假console.log(a);//false假var b = 3 <= 4 || 3 > 1 || 3 != 2;// 声明一个变量 先看3<=4是则为真true 或or 3>1则为真true 后面3!=2则为假后面去反者为真true 俩真者真console.log(b);//true真var c = 2 === '2';// 这里因为全等值要相等还有数据类型也要相等 所以这里结果为false假console.log(c);//false假var d = !c || b && a;// 这里优先级先看与运算 b &&与and a b=true a=false 这里则返回值为false c=false 结果为假!取反则为真trueconsole.log(d);//true真</script>
</head>
<body></body>
</html>
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><script>console.log(4 >= 6 || '人' != '阿凡达' && !(12 * 2 == 144) && true);//这里先看括号里面 !(12 * 2 == 144) 12*2=24==144则为假因为值不同false取反! 为真true// '人' != '阿凡达' true // !(12 * 2 == 144) && true 则结果为真true// '人' != '阿凡达' && true 则结果为真true// 4 >= 6 || true 4 >= 6 结果为false false或or|| true 最终结果为:true真var num = 10;console.log(5 == num /2 && (2 + 2 *num).toString() === '22');// 5 == num /2 这里num=10 10除以2等于5 5==5 则为真true// (2 + 2 *num) 2+2乘以10=22 // 22.toString()转换为字符串‘22’===‘22’ 则结果为true真// 5 == num /2 这里num=10 10除以2等于5 5==5 则为真true&&true 最终结果为true真</script>
</head>
<body></body>
</html>
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><script>var a = 3 > 5 && 2 < 7 && 3 == 4;// 3 > 5 则为假false// 2 < 7 则为真true// 3 == 4 则为假false// 3 > 5 && 2 < 7 false 与 true false与false 结果为false console.log(a);//false假var b = 3 <= 4 || 3 > 1 || 3 != 2;//3 <= 4 则为真true// 3 > 1 则为true// 3 != 2 则为真trueconsole.log(b);//true真var c = 2 ==='2';// 这里全等于就是值相同数据类型也要相同 这里只是数字相同则数据类型不同 则为false假console.log(c);//false假var d = !c || b && a;// 这里先看与运算符 b && a b=true a=false true 与and false 结果为false// !c || false c=false false=false 结果为false在!取反 为trueconsole.log(d);</script>
</head>
<body></body>
</html>