Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Basics (Retired) Making Decisions with Conditional Statements Build a Random Number Guessing Game

Stephan Edmonson
Stephan Edmonson
1,368 Points

Instead of using parseint, in this case can we use the double equal sign to compare the users number with the randomnum?

I just wanted to know if the randomnumber and the users number can be compared using (==) instead of parseint(usernumber)===randomnumber

5 Answers

andren
andren
28,526 Points

The short answer is: yes (but it's not recommended).

The long answer is that these days it's generally considered a bad practice to use the == operator at all. There are many cases where it's auto-conversion of types will lead to unexpected behavior. Take for example this comparison "" == 0, what would you expect that to result in?

You would likely suspect false as an empty string and 0 are not the same thing. But in reality it results in true, why that is the case is a bit more complicated and would require an explanation of how JavaScript treats certain values as "falsy" and some as "truthy". But the point is that there are lots of weird scenarios like that where the result is not what you would expect. Using the === operator leads to far more predictable results.

On top of that it's generally not recommended that you keep a pure number as a string in the first place. As it will result in bugs if that number ends up being used in a math operation later in the program. So using parseInt whenever you are dealing with a pure number should become a habit either way.

Andrew Dovganyuk
Andrew Dovganyuk
10,633 Points

The Dave McFarland, all ready explained why not recomend to use '==' in privious lesson ;)

i agree with andren but if the they are the same value's as 5 and '5' use === if it is 5 and 5 use ==

Stephan Edmonson
Stephan Edmonson
1,368 Points

Thanks, but, I didn't understand that last part that you said. Could you re-explain it?

ok so if have a number let's say 5 than you have a string like this '5' because they are both 5 therefore use === like so if(5 === '5') { //stuff }

but when you use the double equal sign it is alot stricter than the triple equal sign therefore you use == like so

if(5 == 5) {

}

Stephan Edmonson
Stephan Edmonson
1,368 Points

I think it's the other way around no? Isn't the double less strict and therefore the === is called strict?

like if you were gonna make a sniper game you would use the double = sign or the scope wouldn't be steady but if there is a error will using the double = sign the game will freeze