SoFunction
Updated on 2025-03-04

Clever talk about whether 0 in js is true or false

I thought of a fun one, run it as follows:

if ('0') alert("'0' is true");
if ('0' == false) alert("'0' is false");

As a result, it was alerted both times! So is '0' true or false?

The answer is: When comparing js, there are three rules like this:

• If there is a bool among the two compared, the bool will be converted into the corresponding number, namely 0 and 1

• If one of the two parties is number and the other is string, the string will be converted to a number

• When converting string to bool directly, the empty string ‘'' is converted to false, and all other strings are converted to true

The above cliché is whether 0 in js is true or false. This is all the content I have shared with you. I hope it can give you a reference and I hope you can support me more.