javascript (&& ||)表达式

 

javascript中的 && 和 || 不仅可以用做条件判断。

而且还可以用来返回值。

 

 1     var alice = {
 2                 name: "alice",
 3                 toString: function () {
 4                     return this.name;
 5                 }
 6             };
 7             var smith = {
 8                 name: "smith",
 9                 toString: function () {
10                     return this.name;
11                 }
12             };
13             alert(false && alice); //false
14             alert(true && alice);  //alice
15             alert(alice && smith); //smith
16             alert(smith && alice); //alice
17             alert(false || alice); //alice
18             alert(true || alice); //true
19             alert(alice || smith); //alice
20             alert(smith || alice); //smith

总结:(a && b)当a为:(0、""、null、false、undefined、NaN) 条件为false,返回a

    否则返回b ,如果后面有多个&&,直到返回条件为false的值。

    (a || b)  当a为:(0、""、null、false、undefined、NaN) 条件为false,返回b

    否则返回a ,如果后面有多个||,直到返回条件为true的值。

    

 

posted @ 2013-12-15 20:43  无名的小兵  阅读(121)  评论(0)    收藏  举报