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

 

------


你可能感兴趣的:(Opera)