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.
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.
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 @ILM suggested, they all return true, which is confusing
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.
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.
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.
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!
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.
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:)