Why don't logical operators (&& and ||) always return a boolean result?

后端 未结 9 1706
耶瑟儿~
耶瑟儿~ 2020-11-22 06:04

Why do these logical operators return an object and not a boolean?

var _ = (obj.fn && obj.fn() ) || obj._ || ( obj._ = {} );

var _ = obj &&          


        
9条回答
  •  礼貌的吻别
    2020-11-22 06:53

    First, it has to be true to return, so if you are testing for truthfulness then it makes no difference

    Second, it lets you do assignments along the lines of:

    function bar(foo) {
        foo = foo || "default value";
    

提交回复
热议问题