Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Basics (Retired) Making Decisions with Conditional Statements Comparison Operators

difference between != and !==

difference between != and !==

4 Answers

Erik McClintock
Erik McClintock
45,783 Points

Iliya,

The difference is in how strict they are, and exactly what they check for, just like == and ===.

You can get more detailed information in this article about comparison operators.

Erik

the difference is that != is for comparing two different types for example: if you are comparing a variable and a string but it will still work if you comparing a string and a string or a var and a var and what a !== does is only compares the same type so it wont work with a var and a string

Rui W
Rui W
6,269 Points

An example which I think might help you understanding the difference. Please correct me if anyone feel this to be misleading:

7 !== '7' 

This returns true, since left variable is number (TYPE) and right variable is string (TYPE).

7 != '7'

This returns false, since both variable have the VALUE 7. != checks for inequality and found the opposite, so returns false.

!== is strictly comparing variables on both sides for their VALUE and TYPE. The result will be true if either VALUE or TYPE is inconsistent. While != is more "tolerant", that it is going to return true only if VALUE are different from the two sides of comparison.

This can actually be misleading.

I think the easiest way to understand is found at w3school. https://www.w3schools.com/js/js_comparisons.asp

!= will return a false if both meet the criteria and true if they both do not meet the criteria. !== This works similar to !=, the only difference is that this one truly verifies the criteria. Similar to == and ===.

I can see this being used with strings as well. With numbers, it can get a bit confusing.

var x = "hello"
X !== "hello" will return false
X !== "hi" will return true