1 and 0 as booleans in Javascript

Hello.

I was wondering if the numbers 1 and 0 are the only boolean numbers in Javascript, since when I used the equality operator (==) to compare other numbers to the boolean data types true and false, they returned false in both cases.

console.log(1 == true); /* returns true */

console.log(0 == false); /* returns true */

console.log(2 == true); /* returns false */
console.log(2 == false); /* returns false */

Does this mean that, excluding 0 and 1, all numbers cannot be treated as booleans?

Thank you.

Every value can be truthy or falsy, if that’s what you mean with “treated as a Boolean”, check with passing them to the Boolean function

== is a whole other can of worms as it relies on type coercion

1 Like

Some more tests to think about

console.log(1 === true); /* returns false */
console.log(0 === false); /* returns false */
console.log(2 === true); /* returns false */
console.log(2 === false); /* returns false */

These tests all fail because the strict equality operator compares the “type” as well as the “value”. All of the types on the left are numbers and all of the types on the right are booleans, so the types don’t match and thus they are all false.

@bbsmooth, @ilenia, @admit8490

Thank you for the replies!

I understand why using the strict equality operator (===) would result in false, but my question was about whether the numbers 1 and 0 each have innate values of true and false and thus can be used as a boolean.

The reason I ask this:

console.log(1 == true); /* true */ 
console.log(1 == false); /* false */
/* This seems to indicate that 1 has a value of true */

console.log(0 == true); /* false */
console.log(0 == false); /* true */
/* This seems to indicate that 0 has a value of false */

console.log(2 == true);  /* false */
console.log(2 == false);  /* false */
/* Any number besides 0 or 1 results in false,
which seems to mean that they are neither true nor false? */

From this experiment, 0 and 1 behave differently from any other number I use the equality operator on, so I was wondering whether they were “different.”

However, when I used the Boolean function, as @ilenia suggested, they all return true, which is confusing :sweat_smile:

console.log(Boolean(0));
console.log(Boolean(1));
console.log(Boolean(2));
/* All return true */

I’m not familiar with the concepts of “truthy” or “falsy,” so I’ll make sure to look into that.

I apologize if my question was stated in an unclear way; feel free to let me know if there is anything I can improve, since I appreciate the effort people put into answering questions like these to help beginners like me.

Thanks again.

Are you familiar with methods, which can return true or false?
Like hasOwnProperty for example?
I am also a beginner by the way

You can play around this method, write some little code snippets, where can be lines like

if (obj.hasOwnProperty(x) == 1) {
}
....
....

Just for the sake of experiment and better understanding.
In actual code I would not write something like that, because it can be a little confusing)

I would double check the first one.

Complete list of falsy values in javascript.

If a value is in the falsy list then it will return false when coerced to a boolean. Otherwise, it will return true. So any number other than 0 (and technically -0 as well) will return true when you turn it into a boolean.

2 Likes

These sorts of experiments can result in confusing results because of coercion rules.

If the operands are of different types, try to convert them to the same type before comparing:
When comparing a number to a string, try to convert the string to a numeric value.
If one of the operands is a boolean, convert the boolean operand to 1 if it is true and +0 if it is false.

3 Likes

@JeremyLT,

I read through content in the link, and it explains why console.log(2 == true) and console.log(2 == false) returns false.

  • If one of the operands is a boolean, convert the boolean operand to 1 if it is true and +0 if it is false.

The true and false boolean operands are each converted to 1 and 0, respectively, whereas I initially thought the numbers were being converted into booleans. This explains why any number aside from 0 and 1 returns false when compared to either true or false.

This really answered my initial question. Thanks a lot!

And @bbsmooth,

You’re right. console.log(Boolean(0)) returns false. Sorry about that mistake!

And thank you for the link about falsy values in javascript. Truthy and falsy are interesting concepts, and it answers my question of how non-boolean primitives are considered when encountered in a boolean context.

@admit8490,

I am not familiar with methods yet (I’ve just started going through the freeCodeCamp javascript course yesterday), but I’ll definitely look into it. Thank you! Good luck with your coding journey:)

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.