表达式和运算符
-
JS的基本表达式
1算术表达式
2关系表达式
3逻辑表达式
4赋值表达式
5综合表达式-
算数表达式
意义 运算符 加 + 减 - 乘 * 除 / 取余 %
<script> console.log(1 + 2 * 3) // 7 console.log((1 + 2) * 3) // 9 console.log(1 + 2 / 4) // 1.5 console.log(((1 + 2) * 3 + 4) / 2) // 6.5 console.log(1 + 2) // 3 console.log(1 + '2') // '12' console.log('1' + '2') // '12' </script>
加减的符号和数学一致,乘法是*号,除法是/号
默认情况,乘除法的优先级要高于加法和减法;必要时可以使用圆括号来改变运算的顺序加号有“加法”和“连字符”两种作用
如果加号两边的操作数都是数字,则为“加法”
否则为连字符-
取余运算
取余运算也叫作“求模运算”,用百分号“%”表示
a % b表示求a除以b的余数,它不关心整数部分,只关心余数
-
<script>
console.log(11 % 4) // 3, 因为11除以4余数是3
console.log(20 % 6) // 2, 因为20除以6余数是2
console.log(12 % 3) // 0, 因为能够整除,余数是0
console.log(9 % 3) // 0, 因为能够整除,余数是0
console.log(3 % 9) // 3, 因为商0, 余数是3
</script>
-
隐式类型转换
如果参与数学运算的某操作数不是数字型,那么JavaScript会自动将此操作数转换为数字型
<script>
console.log(3 * "4"); // 12
// 隐式转换的本质是内部调用Number()函数
console.log(true + true); // 2
console.log(false + 2); // 2
console.log(3 * "4天"); // NaN
</script>
隐式转换的本质是内部调用Number()函数
-
浮点数运算
在JavaScript中, 有些小数的数学运算不是很精准
0.1 + 0.2 // 0.3000000000000004
JavaScript使用了IEEE754二进制浮点数算数标准, 这会使一些个别的小数运算产生"丢失精度" 问题
解决方法:
在进行小数运算时, 要调用数字的toFixed()方法保留指定的小数位数
- Math.ceil() 向上取整; Math.floor()向下取整
Math.ceil(2.4) // 3
Math.floor(2.4) // 2
Math.ceil(-2.4) // -2
Math.floor(-2.4) // -3
Math.ceil(2) // 2
Math.floor(2) // 2
意义 | 运算符 |
---|---|
大于 | > |
小于 | < |
大于或等于 | >= |
小于或等于 | <= |
等于 | == |
不等于 | != |
全等于 | === |
不全等于 | !== |
判断是否相等
如果要比较两个值是否相等, 此时应该是用 == 运算符
JavaScript中等号= 表示赋值, 而并不是相等. 判断相等应该是用==运算符
-
相等和全等
两个等号== 运算符不比较值得类型, 它会进行隐式转换后比较值是否相等
三个等号===运算符, 不仅比较值是否相同, 也比较类型是否相同
5 == '5'; // true t === '5' // false
-
null和undefined用==进行比较涉及隐式强制类型转换(面试题)
undefined == null // true undefined === null //false
-
! =表示不想等, !==表示不全等
5 != 6 // true 5 !== 6 // true 5 != '5' // false 5 != '5' // true
意义 | 运算符 |
---|---|
非 | ! |
与 | && |
或 | || |
!true // false
!false // true
!0 // true
!undefined // true
!'' // true
!'abc' // false
!!true // true
!!0 // false
!!'' // false
!!'abc' // true
工作中常用!!进行布尔类型的隐式转换, 表示改值的真或加
- 与运算, &&, 表示’并且’, 口诀’ 都真才真’; ‘一假则假’
true && true // true
true && false // false
false && true // false
false && false // false
- 或运算, ||, 表示’或者’, 口诀 ‘都假才假’; '一真为真 ’
true && true // true
true && false // true
false && true // true
false && false // false
-
短路运算(重点, 工作面试常见)
-
a && b // a为真, 表达式结果为b; a为假, 表达式结果为a
3 && 6 // 6 undefined && 15 // undefined 15 && undefined // undefined null && 2 // null '' && 16 // '' NaN && undefined // NaN
-
a || b // a为真, 表达式结果为a; a为假, 表达式结果为b
3 || 6 // 3 0 || 6 // 6 null || undefined // undefined 'a' || 'b' // a NaN || null // Null
-
逻辑运算的优先级是: 非→与→或
-
!true || true // true 3 && 4 || 5 && 6 // 4
意义 | 运算符 |
赋值 | = |
快捷赋值 | += |
-= | |
*= | |
/= | |
%= | |
自增运算 | ++ |
自减运算 | -- |
var a = 6;
a *= 2; // 6X2=12
a += 3; // 12+3=15
a /= 3; // 15÷3=5
console.log(a) // 结果为5
-
自增/自减运算符 ++ 和 – , 表示在自身基础上加1或减1
-
a++和++a的区别 , a++ 先用再加; ++a 先加再用
var a = 3;
var b = a++;
console.log(b); // 3
console.log(a); // 4
var a = 3;
var b = ++a;
console.log(b); // 4
console.log(a); // 4
运算顺序: 非运算→数学运算→关系运算→逻辑运算
5 < 3 + 3 // true
3 > 2 && 8 > 3 + 4 // true
3 > 2 && 8 > 3 + 5 // false
!13 < 5 - 3 // true
!13 < 5 - 5 // false
案例: 闰年判断
公历闰年的简单计算方法 ( 符合以下条件之一即可)
- 能被4整除且不能被100整除
- 能被100整除也能被400整除
// 让用户先输入一个年份
var year = Number(prompt'请输入年份')
// 两个条件判断(满足这两个条件中的一个, 就是闰年):
// 1. 能被4整除且不能被100整除
// 2. 能被100整除也能被400整除
alert((year % 4 == 0 && year % 100 != 0 || year % 100 == 0 && year % 400 == 0))