Friday, September 12, 2008

Javascript: Equality versus Identity

Javascript has 2 basic forms of comparison operators: === and == .== is the standard equality operator while === is the identity operator. The equality operator will perform casting and usually test both sides as strings. The identity operator compares the value each side the type, but it does not work like instanceof.

Recently we did some experiments with the different return values.

The Expressions

ExpressionResult
"test" == "test"
true
"test" === "test"
true
String("test") === "test"
true
new String("test") == "test"
true
new String("test") === "test"false
new String("test") === new String("test")
Before Firefox 3.0.1: true
After: false
new String("test") === String("test")
false

Here are some type tests we performed
ExpressionType
typeof("test")
"string"
typeof(String("test"))
"string"
typeof(new String("test"))
"object"
typeof(function(){})
"function"


What Did we Learn?
First we see that a string is not always a string. Sometimes it's an object. It's easy to forget that Javascript still has the concept of primitives and objects. Integers, Strings and a few others are all primitives. When using the identity operator (===) you have to assume that it is comparing the value of the variable and it's type (as returned by typeof). Further compounding this is the fact that new String() does not type to a "STRING", while calling String as a function does.

What this all suggests is that when dealing with primatives you might as well use the equality operator (==). If you feel it's safer to use the identity operator (===) then you should use String() as a function and never use new String().

13 comments:

Anonymous said...

I recently ran into related issues, and I'd like to point out -- in Firefox 3.0.1 at least -- that new String("zulu") === new String("zulu") evaluates to false, not true. This is what the specification requires. Identity with two objects requires that they be the exact same object.

On the other hand "zulu" === "zulu" returns true.

I'm still searching for a reason why '===' doesn't work when the two objects are both String instances that JavaScript knows about.

I could see '===' failing if the two objects were ZooBar instances that JavaScript knew nothing about.

I suppose they might have done it just to make '===' work the same for all objects, but it still bugs me.

buy dissertation online said...

I'm a B.Tech graduation student. I'm learning java script. This tutorial helps me to learn some new things. Thank you for having this great blog.

for IT the said...

JavaScript Online Training JavaScript Online Training JQuery Online Training JQuery Online Training
JavaScript Course | HTML5 Online Training

Neil Jakson said...

This article gives the light in which we can observe the reality. This is very nice one and gives indepth information. Thanks for this nice article.
dissertation Writing Service

Ella Wilson said...

Thank you so much for your great information, It is too useful for me. kumpulbagi Tunngle supersu

prash p said...

Awesome information.
such an useful article.
thanks for posting.keep sharing.

Aparna said...

The blog explanation is very clear content about this topic. I am surprised to visit your weblog and Thank you...!
Excel Training in Chennai
Advanced Excel Training in Chennai
Unix Training in Chennai
Pega Training in Chennai
Linux Training in Chennai
Job Openings in Chennai
Placement Training in Chennai
Social Media Marketing Courses in Chennai
Power BI Training in Chennai
Excel Training in Velachery

siva said...

Great Blog!!! Was an interesting blog with a clear concept. And will surely help many to update them.
RPA Training in Chennai
RPA course in Chennai
RPA Training Institute in Chennai
UiPath Training in Chennai
Blue Prism Training in Chennai
Machine Learning course in Chennai
RPA Training in Vadapalani
RPA Training in Thiruvanmiyur
RPA Training in Guindy
RPA Training in Anna Nagar

The India said...

iso 27001 certification services
iso 27001 certification in delhi
ISO 9001 Certification in Noida
iso 22000 certification in Delhi

The India said...

iso certification in noida
iso certification in delhi
ce certification in delhi
iso 14001 certification in delhi
iso 22000 certification cost
iso consultants in noida

The India said...

we have provide the best fridge repair service.
Washing Machine Repair In Faridabad
LG Washing Machine Repair In Faridabad
Videocon Washing Machine Service Centre In Faridabad
IFB Washing Machine service centre in faridabad
Samsung Washing Machine Repair In Faridabad
Washing Machine Repair in Noida
godrej washing machine repair in noida
whirlpool Washing Machine Repair in Noida
IFB washing Machine Repair in Noida
LG Washing Machine Repair in Noida

The India said...

we have provide the best ppc service.
ppc company in gurgaon
website designing company in Gurgaon
PPC company in Noida
seo company in gurgaon
PPC company in Mumbai
PPC company in Chandigarh
Digital Marketing Company

The India said...

Rice Bags Manufacturers
Pouch Manufacturers
wall putty bag manufacturers
fertilizer bag manufacturers
seed bag manufacturers
gusseted bag manufacturers
bopp laminated bags manufacturer
Lyrics with music