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
Here are some type tests we performed
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().
Recently we did some experiments with the different return values.
The Expressions
Expression | Result |
"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
Expression | Type |
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().
17 comments:
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.
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.
JavaScript Online Training JavaScript Online Training JQuery Online Training JQuery Online Training
JavaScript Course | HTML5 Online Training
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
Thank you so much for your great information, It is too useful for me. kumpulbagi Tunngle supersu
Awesome information.
such an useful article.
thanks for posting.keep sharing.
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
iso 27001 certification services
iso 27001 certification in delhi
ISO 9001 Certification in Noida
iso 22000 certification in Delhi
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
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
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
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
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
It's very useful post and i had good experience with this salesforce training in Chennai who are offering good certificaiton assistance. I would say salesforce training is a best way to get certified on crm.
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
Great experience I got good information from your blog.
php 7 performance
advertising tips
big data use cases in manufacturing
user licenses in salesforce
android interview questions for fresher
Đặ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
Post a Comment