Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Tuesday, December 15, 2015

What is the difference between (NaN != NaN) and (NaN !== NaN)?

What is the difference between (NaN != NaN) and (NaN !== NaN)?


First of all I want to mention that I know how isNaN() and Number.isNaN() work. I am reading The Definite Guide by David Flanagan and he gives an example for how to check if the value is NaN:

x !== x  

This will result in true if and only if x is NaN.

But now I have a question: why he uses strict comparison? Because it seems that

x != x  

behaves the same way. Is it safe to use both versions or I am missing some value(s) in JavaScript that will return true for x !== x and false for x != x?

Answer by Phil C. for What is the difference between (NaN != NaN) and (NaN !== NaN)?


In this case, the operators work identically. No type conversions are necessary.

On the other hand, and I think I see it happen mostly with the special data types null and undefined, the strict comparison operator can yield unexpected results.

For example, checking if a string is an object

var x = "String";  x == object; //is true because the string is an object due to JS's idea of OOP  

whereas

x === object; //is false because the string is a strict string type, and not a strictly object type.  

Answer by jkdev for What is the difference between (NaN != NaN) and (NaN !== NaN)?


For purposes of NaN, != and !== do the same thing.

However, many programmers avoid == or != in JavaScript. For example, Douglas Crockford considers them among the "bad parts" of the JavaScript language because they behave in unexpected and confusing ways:

JavaScript has two sets of equality operators: === and !==, and their evil twins == and !=. The good ones work the way you would expect.

...My advice is to never use the evil twins. Instead, always use === and !==.

Answer by T.J. Crowder for What is the difference between (NaN != NaN) and (NaN !== NaN)?


...am I missing some value in JavaScript that will return true for x !== x and false for x != x?

No, you're not. The only difference between !== and != is that the latter will do type coercion if necessary to get the types of the operands to be the same. In x != x, the types of the operands are the same, and so it's exactly the same as x !== x.

This is clear from the beginning of the definition of the Abstract Equality Operation:

  1. ReturnIfAbrupt(x).
  2. ReturnIfAbrupt(y).
  3. If Type(x) is the same as Type(y), then

    Return the result of performing Strict Equality Comparison x === y.

  4. ...

The first two steps are basic plumbing. So in effect, the very first step of == is to see if the types are the same and, if so, to do === instead. != and !== are just negated versions of that.

So if Flanagan is correct that only NaN will give true for x !== x, we can be sure that it's also true that only NaN will give true for x != x.

Many JavaScript programmers default to using === and !== to avoid some pitfalls around the type coercion the loose operators do, but there's nothing to read into Flanagan's use of the strict vs. loose operator in this case.

Answer by GOTO 0 for What is the difference between (NaN != NaN) and (NaN !== NaN)?


Just for fun, let me show you an artificial example where x is not NaN but the operators behave differently anyway. First define:

Object.defineProperty(    self,    'x',    { get: function() { return self.y = self.y ? 0 : '0'; } }  );  

Then we have

x != x // false  

but

x !== x // true  

Answer by Benjamin Gruenbaum for What is the difference between (NaN != NaN) and (NaN !== NaN)?


I just want to point out NaN is not the only thing that produces x !== x without using the global object. There are lots of clever ways to trigger this behavior. Here is one using getters:

var i = 0, obj = { get x() { return i++; }};  with(obj) // force dynamic context, this is evil.   console.log(x === x); // false  

As other answers point out, == performs type coersion, but in as in other languages and par the standard - NaN indicates a computation failure, and for good reasons is not equal to itself.

For some reason beyond me people ocnsider this a problem with JS but most languages that have doubles (namely, C, Java, C++, C#, Python and others) exhibit this exact behavior and people are just fine with it.


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 71

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.