return FALSE; $r = well_tag_thread__update(array('id' => $id), $update); return $r; } function well_tag_thread_find($tagid, $page, $pagesize) { $arr = well_tag_thread__find(array('tagid' => $tagid), array('id' => -1), $page, $pagesize); return $arr; } function well_tag_thread_find_by_tid($tid, $page, $pagesize) { $arr = well_tag_thread__find(array('tid' => $tid), array(), $page, $pagesize); return $arr; } ?>javascript - why null is not converted to a boolean value in a conditional statement? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - why null is not converted to a boolean value in a conditional statement? - Stack Overflow

programmeradmin2浏览0评论

Having the following test case: (find fiddle here)

var a = new Date();
var b = null;
var c = {test: "test"};

if(a)
    console.log(a); //--- prints the current date

if(b)
    console.log('null'); //--- never reached

if(c)
    console.log('test'); //--- prints 'test'

console.log(a && b); //--- prints null

Knowing that

console.log(typeof null); //--- prints "object"
console.log(typeof c); //--- prints "object"

I expect the result of

console.log(a && b); 

to be false and not null as it shown in the example.

Any hint?

Having the following test case: (find fiddle here)

var a = new Date();
var b = null;
var c = {test: "test"};

if(a)
    console.log(a); //--- prints the current date

if(b)
    console.log('null'); //--- never reached

if(c)
    console.log('test'); //--- prints 'test'

console.log(a && b); //--- prints null

Knowing that

console.log(typeof null); //--- prints "object"
console.log(typeof c); //--- prints "object"

I expect the result of

console.log(a && b); 

to be false and not null as it shown in the example.

Any hint?

Share Improve this question asked Nov 9, 2015 at 11:53 BeNdErRBeNdErR 17.9k21 gold badges77 silver badges106 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 4

From the MDN:

expr1 && expr2 : Returns expr1 if it can be converted to false; otherwise, returns expr2

new Date can't be converted to false (it's not falsy), so b is returned.

I expect the result of

console.log(a && b);

to be false and not null as it shown in the example.

In many languages with && and || operators (or AND and OR), the result is always a boolean, yes.* But JavaScript's && and || are more useful than that: Their result is their left-hand operand's value or their right-hand operand's value, not coerced to boolean.

Here's how && works:

  1. Evaluate the left-hand operand.

  2. If the value is falsey (coerces to false when we make it a boolean), return the value (not the coerced value)

  3. If the value from #2 is truthy, evaluate and return the right-hand value (uncoerced)

The falsey values are null, undefined, 0, "", NaN, and of course, false. Everything else is truthy.

So for instance

console.log(0 && 'hi');    // 0

...shows 0 because 0 is falsey. Note it resulted in 0, not false. Similarly, the result here:

console.log(1 && 'hello'); // hello

...is 'hello' because the right-hand side isn't coerced at all by &&.

|| works similarly (I call it the curiously-powerful OR operator): It evaluates the left-hand side and, if that's truthy, takes that as its result; otherwise it evaluates the right-hand side and takes that as its result.

The only time you get true or false from && or || is if the selected operand is already a boolean.


* Of course, many (but not all) of those languages (Java, C#) also require that the operands to && and || already be booleans. C's an exception, it does some coercion, but still has the result of the operands as a boolean (or if you go back far enough, an int that will be 1 or 0).

From MDN:

Logical AND (&&) expr1 && expr2 Returns expr1 if it can be converted to false; otherwise, returns expr2. Thus, when used with Boolean values, && returns true if both operands are true; otherwise, returns false.

MDN Documentation

发布评论

评论列表(0)

  1. 暂无评论