Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In javascript, the operator == compares two object with difference type:
32 == '32', it's true, but 32 === '32', it's false.

Here, i don't know how it (==) works, ignore the type ? convert the type ? else ?

Thank U.
Posted

SQL
The identity (===) operator behaves identically to the equality (==) operator except no type conversion is done, and the types must be the same to be considered equal.

Reference: Javascript Tutorial: Comparison Operators

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. It's this case where === will be faster, and may return a different result than ==. In all other cases performance will be the same.

To quote Douglas Crockford's excellent JavaScript: The Good Parts,

JavaScript
JavaScript has two sets of equality operators: === and !==, and their evil twins == and !=. The good ones work the way you would expect. If the two operands are of the same type and have the same value, then === produces true and !== produces false. The evil twins do the right thing when the operands are of the same type, but if they are of different types, they attempt to coerce the values. the rules by which they do that are complicated and unmemorable. These are some of the interesting cases:
'' == '0'           // false 
0 == ''             // true 
0 == '0'            // true  
false == 'false'    // false 
false == '0'        // true  
false == undefined  // false 
false == null       // false 
null == undefined   // true  
' \t\r\n ' == 0     // true 

The lack of transitivity is alarming. My advice is to never use the evil twins. Instead, always use === and !==. All of the comparisons just shown produce false with the === operator.

SQL
Update:
A good point was brought up by @Casebash in the comments and in @Phillipe Laybaert's answer concerning reference types. For reference types == and === act consistently with one another (except in a special case).


JavaScript
var a = [1,2,3]; 
var b = [1,2,3]; 
 var c = { x: 1, y: 2 }; 
var d = { x: 1, y: 2 };  
var e = "text"; 
var f = "te" + "xt";  
a == b            // false 
a === b           // false  
c == d            // false 
c === d           // false  
e == f            // true 
e === f           // true 

C#
The special case is when you compare a string literal with a string object created with the String constructor.
<pre lang="Javascript">"abc" == new String("abc")    // true 
"abc" === new String("abc")   // false 


Here the == operator is checking the values of the two objects and returning true, but the === is seeing that they're not the same type and returning false. Which one is correct? That really depends on what you're trying to compare. My advice is to bypass the question entirely and just don't use the String constructor to create string objects.
 
Share this answer
 
Comments
Prasad_Kulkarni 13-Sep-12 5:27am    
Good work +5! :D
hyjiacan 13-Sep-12 5:51am    
you mean that when == compares, it converts the type in same and === do not do that ?

Now i am wondering what == does, convert the type, or ignore the type ?

By your words, == converts the type, is that ?
Shmuel Zang 13-Sep-12 6:19am    
5'ed.
prashant patil 4987 13-Sep-12 6:21am    
yes..
hyjiacan 13-Sep-12 20:44pm    
got it. thank u.
see below link..
it will helpful for you:
Link 1[^]
 
Share this answer
 
Comments
hyjiacan 13-Sep-12 1:01am    
Thanks, but i want to know how == works,when 32=='32' , why == shows a 'true' ? How js engine works ? How to handle the type ?
 
Share this answer
 
Comments
hyjiacan 13-Sep-12 1:04am    
Thanks, but i want to know how == works,when 32=='32' , why == shows a 'true' ? How js engine works ? How to handle the type ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900