When we compare 10 == "10". JavaScript gives output as true. Does that mean first operand 10 (Number
) gets converted to "10" (String
) or vice-versa?
-
2Was that last sentence supposed to be a question? – melpomene Nov 26 '16 at 11:11
-
1Using the == operator only checks that the two values equal to one another. It totally disregards the type. The === operator checks both the type & value making it a much stricter operator – Jackthomson Nov 26 '16 at 11:13
-
@TheSETJ I guess its not exact duplicate as OP wants to understand how `==` works – Rajesh Nov 26 '16 at 11:27
-
@melpomene , Yes I want to understand how == works while comparing 10 == "10" ( i.e. Number and String in this case ) – K K Nov 26 '16 at 11:40
5 Answers
The ==
operator will compare for equality after doing any necessary type conversions. The ===
operator will not do the conversion, so if two values are not the same type ===
will simply return false
regardless. In our case 10 == "10"
after type conversion both sides of the comparison are equal so it returns true
.

- 18,743
- 3
- 23
- 40
==
means check for values.
===
means check for values as well as type.
so when comparing 10 =='10'
, javascript engine only checks for values, hence true
.
10 === '10'
would yeild false
.
read mozilla's equality comparison for more info.

- 4,289
- 1
- 22
- 32
What double equals to tries to do is, it converts both operands to common datatype. It will try to convert both values into integer. If one of them is converted to int, both will be converted and output will be returned. If both fails, then string comparison will be carried out.
console.log(10 == "10")
console.log(true == "true")
console.log({} == '[object Object]')
The reason second returns false
is because parseInt(true)
will return 1 and parseInt('true')
will return NaN
You can read the algorithm on following link: The Abstract Equality Comparison Algorithm

- 24,354
- 5
- 48
- 79
I don't know if this is exactly a question or not, but if it is; first I need to say your point is correct, a implicit type conversion happens here because == check for equality not being identical and second you may find these links useful:
JavaScript comparison operators: Identity vs. Equality
Which equals operator (== vs ===) should be used in JavaScript comparisons?
JavaScript - === vs == operators performance
Equality comparisons and sameness
Update: I did correct my mistake about conversion and cast, thanks to @melpomene.