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 Foundations Strings Comparing Strings

Dustin Morris
Dustin Morris
8,715 Points

When is the "==" operand the best choice?

If the answer is never, then why even include it in the syntax?

4 Answers

Albert Evangelista
Albert Evangelista
27,686 Points

I use the "==" for testing and debugging javascript codes by letting "possible errors" get into the flow of my codes. It gives me a good view on where to make adjustments. Its like having side mirrors and rear view cameras, you simply see more. Its not the only one you have to deal with if you're going to use javascript.

So you write your code with double in development stage and then change to triple before going live?

Michael Williams
Michael Williams
13,005 Points

Hi Dustin Morris,

It is a great question because it puzzled me just the same when I started to learn JavaScript. It seems that it is common to just say, "this is what I always use" or "this is best practice" without much of an in-depth explanation as to why one would prefer "===" versus "==".

We know that they are both comparison operators:

  • The double is "lenient" and approves of true in the case of the same type; if they are not of the same type, it will attempt to force the two different types to be identical through a conversion.

  • The triple is more "strict" and approves of true only if they are of identical type & value, maintaining the data type integrity.

It seems that developers would approve of the latter to not take the risk of the "JavaScript language rules" deciding if it is permissible to auto-convert the output by using the double == and go with the triple === at face value, thus always guaranteeing predictable, expected, obvious results (no favors on the browser's part). The same rules apply to != and !==.

Best Regards, Michael

Damn! These are good answers. I've often wondered this myself. Thanks Michael and Albert.

Emmanuel Koranteng
Emmanuel Koranteng
21,488 Points

True, it seems very confusing as a beginner in JavaScript. Good answers, thanks guys!