逻辑运算符
布尔运算符
与(并且)
符号:&& and符号(&一个符号表示位运算符)
书写方式:表达式1 && 表达式2
1.将表达式1 进行 boolean判定
以下数据均判定为false:
1)null
2)undefined
3)false
4)NaN
5)''
6)0
其他数据全部为true
2.如果表达式1的判定结果为假,则直接返回表达式1,而不执行表达式2,否则,返回表达式的结果
console.log(5>3 && 3>2);//true
console.log(5>3 && 100);//100,第一个表达式为真,则直接返回第二个表达式的值
console.log(0 && 2>1);//0
//面试题
var x=1;
console.log(x>2 &&x++>0);//false,第一个表达式为假,直接返回表达式1
console.log(x);//1
console.log(1&&2&&3&&4&&0);//0
var age=-1;
age<0 && (age=0)//这里&&左边的表达式结果为false,所以执行&&右边的表达式,将age的值设置为0,但是这个代码难以阅读和理解 推荐使用三目运算
console.log(age,'age');//0
var x=1;
console.log(x++>=1 && x++>=2 &&x++>=4 &&x++>4);//false
console.log(x);//4
或
符号:||
写法:表达式1||表达式2
1.将表达式1进行boolean判定
2.如果表达式1为真,直接返回表达式1,不运行表达式2;否则,返回表达式2
console.log(1>3||10);//10,1>3结果为假,返回表达式2
console.log(undefined||'abs');//abs,undefined转换为boolean为假,直接返回表达式2的值
console.log(0||null||undefined||null||NaN);//NaN
console.log(0||null||undefined||null||1||NaN);//1
var o={
name:'test',
gender:'female',
age:0
}
//对象中如果没有年龄,则将对象赋值为18
o.age===undefined||18
console.log(o);
非
符号:!
写法:!数据
一元运算符
将数据的boolean判定结果直接取反,非运算符一定返回boolean类型
console.log(!true);//false
console.log(!undefined);//true
console.log(!2+1);//1
// 判断闰年
// 在变量中存放年份
var year=2000;
//用逻辑运算,判断当年是否是闰年:闰年规则:四年一闰,百年不闰,400年一闰
console.log(year%4==0&&year%100!=0||year%400==0);
//如果湿润年,则输出闰年,否则输出平年
if(year%4==0&&year%100!=0||year%400==0){
console.log('闰年');
}else{
console.log('平年');
}
练习1:用一个变量保存成绩,输出该成绩是否及格(true 或 false)
var score=90;
// 方法1
// score>=60&&console.log('恭喜你及格了');
// score>=60||console.log('哇哦,没有及格哦,下次继续努力');
// 方法2
score>=60?console.log('恭喜你及格了'):('哇哦,没有及格哦,下次继续努力');
// 方法3
if(score>=60){
console.log('恭喜你及格了')
}else{
('哇哦,没有及格哦,下次继续努力')
}
练习2:用一个变量保存年份,得到该年份2月的天数
var year=2000;
//如果是闰年,2月有29天,如果是平年,只有28天
function numberDaysOfFebruary(year) {
if(year%4==0&&year%100!=0||year%400==0){
return 29
}else{
return 28
}
}
console.log(numberDaysOfFebruary(year));
练习3:利息计算器
设置变量,分别存放本金、月数、年利率,计算利息
如果本金存放量超过了10万,年利率上浮20%(比如,年利率为4%,上浮后的年利率 4% * 1.2)
var money = 100000,
month = 3,
rate = 5.6; //年利率百分比
function calculationInterest(money,month,rate){
if(money<100000){
return money * rate / 100 / 12 * month
}else{
return money * rate*1.2 / 100 / 12 * month
}
}
console.log(calculationInterest(money,month,rate));
// 利息公式:利息=本金*存期*利率
补充知识
模板字符串
//模板字符串
var user={
name:'xiaoxiao',
age:18,
gender:'男'
}
// 输出:我叫XXX,今年xxxx岁了,性别是xxx
// 以前的做法
// console.log('我叫'+user.name+',今年'+user.age+'岁了,性别是'+user.gender);
//使用模板字符串
console.log(`我叫${user.name},今年${user.age}岁了,性别是${user.gender}`);
类型转换你不会影响原本的数据
类型转换只发生在运算的时候,并不会改变参与运算元素原来的值和类型
var x='1';
var y=x*2;
console.log(y,typeof y);//2 'number'这里计算的时候x会发生隐式类型转换,得到结果为number类型的2
console.log(x,typeof x);//1 string,计算完之后x的类型还是string,并没有发生改变
复合的赋值运算符
+=,-=,=,/=,%=,*=
var x=2;
// x+=3;//等同于 x=x+3
// x*=1+2;//等同于 x=x*(1+2)
x*=x++ + 2;//等同于x=x*(x++ + 2) x=2*(2+2)=8
console.log(x);
typeof运算符
一元运算符,typeof运算返回表达式的类型,是一个字符串
写法:
1.普通写法:typeof 表达式
2.函数写法:typeof(表达式)
typeof的优先级比较高,日常开发中建议使用函数写法,避免造成不必要的错误,例如:
console.log(typeof 1+2);//'number2'
console.log(typeof(1+2));//number
结果完全不同
void运算符
一元运算符:运行表达式,然后返回undefined
写法:类似于typeof运算符
1.普通写法:void 表达式
2.函数写法:void(表达式)
void的优先级也比较高,日常开发中建议使用函数写法,避免造成不必要的错误,例如:
console.log(void(0));//undefined
console.log(void('aaaa'));//undefined
console.log(void 1+2);//NaN
console.log(void(1+2));//undefined
逗号运算符
写法:表达式1,表达式2
依次运行两个表达式,返回表达式2
面试题
var x=1;
x=(x++ * 2,x++ * 2,x++ *2)
console.log(x);//6
都好运算符的优先级比赋值运算符更低
var x=1;
x=x++ * 2,x++ * 2,x++ *2;//这里其实先执行了x++ * 2把结果直接赋值给了x,执行第二个表达式的时候x自增1,执行第三个表达式的时候x的值再自增1,所以就变成了4
console.log(x);//4
网友评论