Over a million developers have joined DZone.

Stricter Equality in JavaScript: ECMAScript.next's Solution

DZone's Guide to

Stricter Equality in JavaScript: ECMAScript.next's Solution

· Web Dev Zone
Free Resource

Add user login and MFA to your next project in minutes. Create a free Okta developer account, drop in one of our SDKs to your application and get back to building.

Most JavaScript programmers are aware that “normal” equality (==) should be avoided in favor of strict equality (===) [1]. However, every now and then you need something even stricter than ===: If you want to check for NaN or if you want to distinguish between -0 and +0. This blog post explains the details and ECMAScript.next’s [2] solution, the “is” operator.

Checking for NaN

NaN is not strictly equal to itself, breaking the fundamental law of reflexivity: In mathematics, any value x is always equal to itself:

    x = x 

That law doesn’t hold for === and NaN:

    > NaN === NaN

As a consequence, you can’t find NaN in arrays via indexOf, because that method relies on === to determine which array elements are equal to its first argument: 

    > [ NaN ].indexOf(NaN)

If you can’t use === to check for NaN, what is the alternative? There is the global function isNaN(), but it is problematic, because it converts its argument to number and thus returns true for many values that clearly are not NaN [3]

    > isNaN("foo")

Explanation: "foo" converted to number is NaN. 

    > Number("foo")

One way to detect NaN is to exploit the fact that it is the only value that isn’t strictly equal to itself: 

    function myIsNaN(value) {
        return value !== value;

A more self-explanatory alternative is to use isNaN() after checking whether the value is a number. This avoids the problematic conversion of non-numbers [3]

    function myIsNaN2(value) {
        return typeof value === 'number' && isNaN(value);

ECMAScript.next will have Number.isNaN(), a fixed version of the global isNaN().

Distinguishing between -0 and +0

This use case is rare, but sometimes you might want to distinguish between -0 and +0 – which are distinct values in JavaScript. === does not let you do that:

    > -0 === +0

How can you make the distinction? It turns out that there is one number that you can divide by 0 – the number 1. Dividing 1 by -0 results in -Infinity, dividing it by +0 results in Infinity. And the two infinities can be distinguished by ===: 

    > 1 / -0

    > 1 / +0
    > Infinity === -Infinity

Stricter equality in ECMAScript.next: the “is” operator

ECMAScript.next will have an “is” operator that performs “stricter equality”: It considers NaN equal to itself and distinguishes between -0 and +0. Its negation is called “isnt”. Examples:
    > NaN is NaN
    > -0 isnt +0

The operator is complemented by a function Object.is() that can be back-ported to older versions of ECMAScript. On those versions, it could be implemented as follows (a slightly edited version of the ECMAScript.next proposal): 

    Object.is = function(x, y) {
        if (x === y) {
            // x === 0 => compare via infinity trick
            return x !== 0 || (1/x === 1/y);

        // x !== y => return true only if both x and y are NaN
        return x !== x && y !== y;

Why does the ECMAScript.next proposal use the name “egal”?

The reason that the ECMAScript.next proposal uses the name “egal” for the “is” operator is due to the seminal paper on equality, “ Equal Rights for Functional Objects or, The More Things Change, The More They Are the Same” by Henry G. Baker, 1990. Quoting that paper’s explanation for calling its equality operator “egal”:

Egal is the obsolete Norman term for equal, and Égalité is the French word for social equality.

Trying out Object.is()

If you want to try out Object.is(), you can use the es6-shim [4] that backports some of ECMAScript.next (which is the current code name for ECMAScript 6) to ECMAScript 5.


  1. Equality in JavaScript: === versus ==
  2. ECMAScript.next: the “TXJS” update by Eich
  3. NaN and Infinity in JavaScript
  4. es6-shim – ECMAScript 6 functionality on ECMAScript 5


Launch your application faster with Okta’s user management API. Register today for the free forever developer edition!


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}