JavaScript Basics: Object.is

in javascript •  7 years ago  (edited)

You may have heard of the book JavaScript: The Good Parts (Yahoo Press, December 2008) by Douglas Crockford. The name, obviously, implies that JavaScript has bad parts, too. The equality operator == is one aspect of JavaScript that is considered a bad part. Crockford writes in this book:

“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.”

So the problem with the == operator is that it performs unexpected and complicated type conversions. For this reason, it is recommended that you always use === and never use ==.

Object.is is yet a new addition to JavaScript that tests for equality. It behaves like the === operator, with two exceptions: Contrary to the === operator, it treats +0 and -0 as not equal, and it treats NaN and Number.NaN as equal. Otherwise, it behaves like ===. In other words, it returns true if both operands are undefined, both are null, both are true or false, both are strings with the same length and the same characters, both are the same object, or both are numbers with the same value.

Here are a few examples:

    Object.is("hi", "hi"); // true
    Object.is(undefined, null); // false
    Object.is("", false); // false
    Object.is(+0, -0); // false
    +0 === -0; // true
    Object.is(NaN, Number.NaN); // true
    Number.NaN === NaN; // false

It is evident that Object.is is the best way to test for equality in JavaScript.


Related Posts

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

java programming good technology for every people.all kind of people to know important about java programming....thank you sir for your good post..@ghasemkiani

Great tip about is operator. Thanks for such a useful post.

I have done level 4 of my game in ethereum. Alhamdulillah In the mean time I got a job. So i am very happy and also feeling sad because now i will get less time to connect with you. :(

So you're developing a game for ethereum. That's very interesting. Wishing you good luck and success!

Trying to do so sir. A game which will support ethereum.

I have done core java , how much time will it take me to study basics of javascipt ?

JavaScript is very easy to learn, especially since you already know Java, you are familiar with its syntax. But you have to grasp two important concepts about JavaScript. First, the fact that JavaScript is a functional programming language and functions are first-class citizens here. Second, you should understand the prototype inheritance concept. I think these are the most important differences between JavaScript and Java. Besides, you should know that being a scripting language, JavaScript needs a host environment. It is usually run in the browser for client-side programming, but it is also widely used for server-side programming, notably with NodeJS. There is also Nashorn which is part of JDK 8 and later, which enables you to run JavaScript programs on top of Java. It's actually very cool. You can have the advantages of both Java and JavaScript all at once, and it is very fast too.

love to read it. thanks@ghasemkiani:-)

How are you sir @ghasemkiani? Excellent tutorial program of Javascript.It is very helpful for me as I am learning in a training program on Web Designing.
Thanks @ghasemkiani for sharing.

Thanks.

Excellent job sir. Thanks for the examples. It makes more easy to me to understand java programming.

Your post is so excellent

Nice tips about java script. Thanks for sharing such tips.

ahsant

Thank you sir for nice post........

Good Post, thanks for sharing it.

Good to see your new experiment about JavaScript has worthy information always.

Assalamualaikum sir

I haven’t started java yet but I could pick one or two from you.

thanks for the info

Bisyar khub :)

Carry on my dear sir&visit @pranto

great job dear and very well post. thank you for shering tihs post

good news. thanks for shairing

well done ghasemkiani!

java programming good technology for every people.all kind of people to know important about java programming....thank you sir for your good post..@ghasemkiani

Nice information about java

this is really helpful blog. excellent program of javascript. thank you so much for sharing this post.

leran new thing from you. i have knowladge about c language

java is a useful program. thanks for sharing.

Informative post on java.

a good post my good friend @ghasemkiani thanks for the information that has been shared with us

Thanks for sharing this post..I appreciate programing ..Thanks for share this post..

your posts help us to know about java programming.. Great job sir. Thank You and keep it up @ghasemkiani

Carry on your activitys

Thanks for this post sharing.... i appreciate this programming...

very informative post...thanks sir

very miss you dear @ghasemkiani

@ghasemkiani Yes, JavaScript: The Good Parts is a great book. I just love your series of JavaScript. Keep sharing.
giphy.gif

thanks for sharing programming technology.
keep it up

am not heard

Really good way to make equally command.

This nice post i like it thanks for sharing this technology.. Best of luck..

i like reading book's thank you dear for sharing

i'm very sorry dear,, I did not come to the blog because I was in the examination,,,, very miss you dear,,

Thank you sir
You learnt me many thing about javascript in easily
Very very thank you

Your blog always halpful

keep it up learning and exploring

Thanks, but i am a liltle confuse

very well post and great writing dear @ghasemkiani i like and support your post all time

keep it up dear
@ghasemkiani

Great educational tips.Complete a series sir.
Thank you

i wanna knew more about it
Can i?

its so helpful post for me. thanks for sharing