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().

10 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.

writing tips 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

lokesh said...

This blog contains useful information. Thank you for deliverying this usfull blog..
Spoken English Classes in Bangalore
Spoken English Classes in Chennai
English Speaking Course in Bangalore
Best Spoken English Classes in Bangalore
Spoken English in Bangalore
Spoken English Classes in Marathahalli
AWS Training in Bangalore
Data Science Courses in Bangalore
DevOps Training in Bangalore
DOT NET Training in Bangalore

swaroop said...

One of the best blogs that i have read still now. Thanks for your contribution in sharing such a useful information. Waiting for your further updates.

Web Designing Training in Chennai

Web Designing Course in Chennai

Web Designing Training in Bangalore

Web Designing Course in Bangalore

Web Designing Training in Hyderabad

Web Designing Course in Hyderabad

Web Designing Training in Coimbatore

Web Designing Training

Web Designing Online Training

Vé máy bay từ Canada về việt nam said...

Đặt vé máy bay tại Aivivu, tham khảo

vé máy bay đi Mỹ hạng thương gia

đặt vé máy bay giá rẻ từ mỹ về việt nam

mua vé từ nhật về việt nam

vé máy bay từ đức về việt nam giá rẻ

vé máy bay từ canada về việt nam bao nhiêu tiền

vé máy bay incheon hà nội

danh sách khách sạn cách ly tại hà nội

iteducationcentre said...

Great Post.
also, check Java classes in Pune