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 trialCarlos Ramirez
5,084 PointsAbout === instead of ==
I don't understand in order to check the correct answer why === instead of == is used.
3 Answers
Dom Farnham
19,421 PointsHi,
They are both comparison operators. However, they are slightly different.
== is equal to
=== is equal to and same type
var cars = 20;
cars == 20 // true
cars === 20 // true
cars == "20" // true
cars === "20" // false
Hope this helps. :-)
Saiteja Vemula
7,162 Points== is just an equals to operator where as === is strict equals to operator. == evaluates to true if both operands have same data even though they differ in type.
For example :
var x = 3;
vay y = '3';
if (x===y) {
document.write("x is not equals to y");
} else if (x==y) {
document.write("x is equals to y");
}
In this example, the condition in "if" is False, where as the condition in "else if" is True
Carlos Ramirez
5,084 PointsOk, now I understand the difference. But why is === instead of ==?. In the case of the video he is using === to compare two strings. Why not simply using ==? Is there any best practice about this?
Saiteja Vemula
7,162 Points== will evalute to true even if the variables are of different types. Where as === doesn't. === evaluates to true only if both the operands are exactly same(i.e., both value as well as type).
Dom Farnham
19,421 PointsI use === because I prefer strictly typed. People who come from using strictly typed languages such as C, may prefer to use === as opposed to the looser ==.