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 JavaScript Basics (Retired) Making Decisions with Conditional Statements Comparison Operators

Michael Plemmons
Michael Plemmons
9,393 Points

converting input before comparing.

One of the things I learned from java is before comparing 2 things like integers, if one's not an integer you need to convert via the parseInt() method. For instance, when asking a user for input, it stores it as a string, then you need to convert it to an integer, then compare the values. Otherwise you'll get an error while compiling because the data types don't match.

Would this still be the best practice for numbers in javascript instead of using == to compare 2 types that aren't the same data type?

1 Answer

It kinda depends on the context. JavaScript's type coercion will allow you to write less code in some contexts but in other contexts... you'll be pulling your hair out due to an abundance of WTF moments.

Also, the typeof method is your friend when trying to figure out a particular data type.