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 Variables Null and Undefined

Difference between == and ===

1) What's the difference between == and ===?

Could you show me two examples using if statement?

2) Why is undefined === null returns false and undefined == null returns true?

4 Answers

Jeff Jacobson-Swartfager
Jeff Jacobson-Swartfager
15,419 Points

The difference is in how strict they are in comparing the equality of the values. MDN has a good reference on this.

== is less strict than ===. A good example of this is 1 vs "1":

1 == "1" 
> true

1 === "1"
> false

1 is a number. "1" is a string. In the first comparison, it evaluates to true because it converts both values to the same type before doing the comparison. The second comparison is comparing the value type as well; since they are not the same type, they are not the same.

Jeff Jacobson-Swartfager
Jeff Jacobson-Swartfager
15,419 Points

Similarly, undefined == null returns true because the comparison converts the two values to the same type first. undefined === null returns false because the comparison compares the value types, which are different.

MDN on undefined. MDN on null.

Thank you! It was very helpful.

Dean McKenzie
Dean McKenzie
11,149 Points

Thanks Jeff nicely explained!

Ben H
Ben H
1,114 Points

== tests that the values are the same === tests that the values AND types are the same