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 Basics Making Decisions in Your Code with Conditional Statements Comparison Operators

'10' == 10 returns true, '10' !== 10 returns true also.

can someone explain why is it so? Thank you.

Blaize Pennington
Blaize Pennington
13,878 Points

So there are two types of 'equals' in Javascript.

== and != are for equality. This means that the two values are equal.

=== and !== are for identity. This means that the two values are exactly the same.

10 == '10'
//returns true
10 === '10'
//returns false

10 != '10'
//returns false
10 !== '10'
//returns true

'10' is a string and 10 is an integer. So they are equal (both are 10), but they are not identical.

1 Answer

The 10 == '10' expression returns true because the equality operator (==) checks to see if two operands are equal regardless of the operand type. As such, the code is able to determine that the integer 10 is equal to the string 10 mathematically. The strict not equal (!==) operator will return true if the operands are not the same type. A better comparison of these operations would be the strictly equal (===) operator and the strict not equal (!==), which will return the results you likely expected.