JavaScript Basics: Object.is

in #javascript7 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

Sort:  

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

Coin Marketplace

STEEM 0.23
TRX 0.22
JST 0.037
BTC 98660.69
ETH 3408.24
USDT 1.00
SBD 3.18