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 trialEvan Shellborn
4,827 PointsDouble equals vs Triple equals comparison
In this video, Randy says that we shouldn't use a === because we're going to always have the same data type anyways. But wouldn't it be faster to use a === than a == because it wouldn't have to do any "data juggling", we are already telling the compiler that it's the same data type?
1 Answer
Stone Preston
42,016 Pointswe are already telling the compiler that it's the same data type?
=== does not tell the compiler the two values are the same type, it checks to make sure they are in addition to checking the values.
=== will check the value AND the data type.
== just checks the value, and type casts if necessary.
=== would be faster in the situation where the types are different, since it would just return false instead of casting them and then comparing the values like the == would. In the other cases performance would probably be very similar.
from this page
When comparing two variables of same data type, both operator takes almost equal time. But if type are not same triple equals operator will be faster because it would not try to convert the types of variable causing it sooner to exit than double equals operator.
Evan Shellborn
4,827 PointsEvan Shellborn
4,827 PointsSo when comparing two numbers, would == be any faster?
Evan Shellborn
4,827 PointsEvan Shellborn
4,827 PointsI thought I read an article once that you should always use === because it's faster.... That was for javascript, not sure if it's any different
Stone Preston
42,016 PointsStone Preston
42,016 Pointssee the article I linked above. it has a section on the performance gains. it does say to always use ===, but speed is not the reason. It is to prevent unexpected things from occurring.