码迷,mamicode.com
首页 > 其他好文 > 详细

短路运算符

时间:2017-01-25 18:47:03      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:bsp   null   span   nbsp   nan   bool   fine   返回   undefined   

js中的短路运算符主要是&&和||

&&

1.左边的值经过boolean运算为true,返回右边的值

eg: 

var a= 1 && "bb";

console.log(a); //bb

2.左边的值为null、undefined或NaN,返回null、undefined或NaN

eg:

var a= null && "34343";

console.log(a); //null

3.左边的值经过Boolean运算为false,返回左边的值

eg:

var a= 1;

var b= a> 10 && a< 5;

console.log(b); //false

--------------------------------------------------------------------------------------------------------------

||

1.左边的值Boolean运算为true,返回左边的值

eg:

var a= 5 || 6;

console.log(a);

2.左边的值Boolean运算为false,返回右边的值

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

3.左边为null、undefined、NaN,返回右边的值

eg:

var b= NaN || undefined;
console.log(b); //undefined

短路运算符

标签:bsp   null   span   nbsp   nan   bool   fine   返回   undefined   

原文地址:http://www.cnblogs.com/yanze/p/6349851.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!