Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

JR Lee
JR Lee
4,970 Points

( 0 == "b" ) returning false, but it should be true?

Hi, I tried running ( 0 == "b" ) in console, and it returns false, but as the link "Why JavaScript treats 0 equal to an empty string" in the teacher's notes explains, using == operator on a comparison with 2 different operands, if one of the operands is a number, the other operand would convert to a number, in this case b should convert to a number by default, which should be 0. Can someone please explain why then ( 0 == "b" ) would log false? Thank you!

1 Answer

Steven Parker
Steven Parker
231,269 Points

The string would be converted to a number if it contains only digits. So for example, 0 == "0" would be true.

But since "b" isn't a digit, and is also not an empty string, it cannot be converted to a number and will not be equal to any number.