相关文章推荐
没有腹肌的甘蔗  ·  迴圈範例·  1 年前    · 
阳光的金鱼  ·  可能是最全的开源 LLM ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I was convinced that any logical expression in Javascript will return a boolean value, yet this expression returns a number 0 instead of a bool:

0 && true

Why is that so? How do I approach logical expressions in Javascript in this case to prevent this kind of mistake in the future?

Background story - I was baffled by this statement in jQuery:

$('.something').toggle(0 && true);

It doesn't toggle the element because '0' is returned, and not a boolean!

Maybe there are some clever reasons why this is so, but can't say I like it.

what about Boolean(whatever complex expression you want) ... i.e. in your case Boolean(0 && true) - not only will it result in a Boolean, it's "self documenting" - here there be a Boolean, let no one be mistaken – Jaromanda X Sep 14, 2018 at 6:50 Can you show an example where it would be a mistake? Without a complete example it is hard to tell how to do that in the correct way. 0 && trueevaluates to 0 because 0 is the first falsy part of the expression, so it would work correctly in case of an if clause. – t.niese Sep 14, 2018 at 6:56 @t.niese yes I'd argue that .toggle is the problem rather than JS, since it accepts all sorts of values. If you pass in a boolean, you either show or hide the element. Passing in a number sets a delay. Passing a string will be treated as being a number. Passing an object will be treated as if it's settings. Passing undefined will be akin to invoking just .toggle() with no arguments. A function will be executed after completion. I don't know what happens if you pass in null - presumably it'd be treated as false...or an object. It's convoluted. – VLAZ Sep 14, 2018 at 7:44

This is why is returns the first value: 0

Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators#Description

You expected as a result false (a boolean), however the boolean value of the resulting value is falsy too. This means that you can use it in a condition (if) and have the expected behavior.

If you need to store it in a variable and prefer to have a boolean value, you can convert it. This can be done using a double negation: !!

!!(0 && true)

Or using Boolean:

Boolean(0 && true)

As @Maxime Chéramy noted, JavaScript checks if the first element returns false and it doesn't really check the second one. This saves calculation time.

JS does not need to check both statements if the first is false it will not check the second one, it will just return the first one. If the first is true, again, it doesn't check the second one, it just returns it.

A number, object, array, or string are truety.

Use Boolean() to get a Boolean value or use the short method !!()

'banana' && true  // returns true
true && 'banana' // returns 'banana'
Boolean(true && 'banana') // returns 'true'
!!(true && 'banana') // returns 'true'

with three

true && 'banana' && 1 // returns 1
true && 1 && 'banana' // returns 'banana'
1 && 'banana' && true // returns true
Boolean(true && 'banana' && 1) // returns 'true'
!!(1 && 'banana' && true) // returns 'true'
                I don't know OP knows how it works, or they probably wouldn't have asked the question as is. Also, even if OP knows that, what about somebody else who stumbles upon the question?
– VLAZ
                Sep 14, 2018 at 7:09
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.