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 trialGary Calhoun
10,317 PointsSo is there ever a scenario when we would use the == and !=?
I understand that we use === and !== but what is the purpose of having other operators if we don't use them.
Is there ever a time when it may be best to use != or == over !== and ===?
5 Answers
Ken Alger
Treehouse TeacherGary;
Robert Richey did a nice job, but let me add a couple of things.
var1===var2
When using three equals signs for JavaScript equality testing, everything is as is. Nothing gets converted before being evaluated.
JavaScript triple equals image
var1==var2
When using two equals signs for JavaScript equality testing, some, um, interesting conversions take place.
Moral: Use three equals unless you fully understand the conversions that take place for two-equals.
Happy coding,
Ken
Robert Richey
Courses Plus Student 16,352 PointsHi Gary,
You may consider using ==
over ===
when you're aware that JavaScript will coerce the type to check for a truthy value. For example,
var number = 5;
var string = '5';
if (number == string) {
console.log('type coercion: string value "5" can cast to number value 5')
}
if (number === string) {
/* nope */
} else {
console.log('types do not match. values do not matter in this case')
}
From what I've read from other more esteemed developers, the best practice is to always use strict equality ===
. This makes the intent of the code easier to understand and debug - not wasting time trying to figure out what the type is supposed to be.
Hope this helps,
Cheers
Gary Calhoun
10,317 PointsOk so its best practice because it has more specificity (css lol) I understand now so always use the strict version to make the code easier on myself in the long run. Thanks
Rob B
11,677 PointsI have to get my head wrapped around this, and it might take some time. I have been studying C++, C#, and Java. None of them use a ===, only a single or double.
Nguyen Quach
1,827 PointsI'm still not seeing any practical use for using == or !=. It just seems worthless since everyone recommends strict equality's. How is this beneficial for CSS?
Justin Blalock
8,206 PointsSeems like == can be used for light forms that can take light fluctuations that come with the conversion. ( Having a blank entry being the same as false for example ). === is definitely 1 jillion times easier to debug since it seems to follow the "if and only if" idea ( passwords definitely come to mind here ). As for != (not equal), I have always found that operator useful for repetitions (aka loops).
Gary Calhoun
10,317 PointsGary Calhoun
10,317 PointsOh wow thanks I see exactly what you mean, that 2nd image really says it all:)
Ken Alger
Treehouse TeacherKen Alger
Treehouse TeacherAs a bit of an add on, to be a well versed JavaScript developer, fully understanding those conversions is likely a prerequisite.
Ken
Kevin Pryce
3,051 PointsKevin Pryce
3,051 PointsWow! This is a great resource! Thanks for sharing.