js logic operator

logical expression of js

 

&& ,||, !

 

operand:

the 3 operator all accept any values as operand,

 

return value:

&& and || return one of the 2 operand as return value,

! always return true or false as return value,

 

------

truthy & falsy value

 

falsy value:

false, null, undefined, 0, -0, '', NaN,

 

truthy value:

any other value,

 

------

&&

 

logic:

when left value is falsy, return left value,

when left value is truthy, return right value,

 

short circuit:

if left value is falsy, then the right value will not be evaluated,

 

e.g.

var a=1;

var b=2;

var c=0;

var d=false;

console.log(a && b); // => 2

console.log(a && c); // => 0

console.log(a && d); // => false

console.log(c && b); // => 0

console.log(c && d); // => 0

console.log(d && c); // => false

 

------

||

 

logic:

when left value is truthy, return left value,

when left value is falsy, return right value,

 

short circuit:

if left value is truthy, then the right value will not be evaluated,

 

e.g.

var a=1;

var b=2;

var c=0;

var d=false;

console.log(a || b); // => 1

console.log(a || c); // => 1

console.log(a || d); // => 1

console.log(c || b); // => 2

console.log(c || d); // => false

console.log(d || c); // => 0

 

------

!

 

logic:

if value is truthy, return false,

if value is falsy, return true,

 

e.g.

var a=1;

var b=2;

var c=0;

var d=false;

console.log(!a); // => false

console.log(!b); // => false

console.log(!c); // => true

console.log(!d); // => true

 

------


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值