DEV Community

Cover image for Equality Algorithms in JavaScript
Ahmed Mohamed
Ahmed Mohamed

Posted on

Equality Algorithms in JavaScript

In this article, we will know what is Equality Algorithms and How we can use them in JavaScript.

What are Equality Algorithms?

Equality Algorithms are the Techniques that JavaScript uses to check if the First value equals the Second Value.

So now we know what Equality Algorithms know let us know how to use them.

There are four algorithms to check equality in JavaScript:

  1. Loosley Equality Algorithm ( == )

  2. Strict Equality Algorithm ( === )

  3. Same Value Equality Algorithm ( Object.is() )

  4. Same Value Zero

Know Let us talk about each of them step by step:

  1. Loosely Equality Algorithm ( == )

    console.log("1" == 1) // true
    console.log(false == 0) // true

Look at the above code, We can see that the result is true although the data types of each one are different so what is going on here?

The secret in the == operator, when you check the equality of two different types in JavaScript this operator automatically converts one of the two values and then compares them and this is what call Implicit Type Coercion.

So the == equality operator makes Type Coercion in we use it and Because of this, we called it Loosely Equality Operator.

2. Strict Equality Algorithm ( === )

This Equality Parameter behaves like the previous one, The Only Difference is that it doesn't make any type of conversion in the values so it gives you the exact thing that you want and because of this, you should use this Equality Parameter instead of Loosly Equality Parameter.

So if we repeat the previous example with === Parameter this will be the result:

    console.log("1" === 1) // false
    console.log(false === 0) // false

    console.log(1 === 1) // true
    console.log(false === false) // true
Enter fullscreen mode Exit fullscreen mode

3. Same Value Equality Algorithm

In JavaScript that let you check if the First Value is the same Other Value.

But Wait…Is not **===** Operator do the same thing

Are You Sure?? Let us see the following example first:

console.log(NaN === NaN) // false
Enter fullscreen mode Exit fullscreen mode

What Happened here is that NaN cannot be compared to another NaN in JavaScript so **Object.is()** Solved this Problem.

It checks if the First value if it has the same value as the Second value.

So if We Repeated the previous example with **Object.is()** we will get the desirable Results

console.log(NaN === NaN) // false
Enter fullscreen mode Exit fullscreen mode

But keep in your mind these exceptions for using It :

Object.is(-0, +0) // false 
    console.log(-0 === +0) //true

    // ---

    Object.is(NaN, NaN) // true
    console.log(NaN === NaN) // false

    // ---

    const obj1 = {name: "Ahmed", age: 22}
    const obj2 = {name: "Ahmed", age: 22}

    console.log(Object.is(obj1, obj2)) // false
    console.log(obj1 === obj2) // false

    /* 
      In this example two methods gave the same result but there are different
      in there tichneeques:

      [1] === when compare Objects checks if the two Objects refer to the same Reference
      [2] Object.is() when compare Objects checks if the two Objects have the same values
    */
Enter fullscreen mode Exit fullscreen mode

Strict Equality Operator is widely used but there are situations that the **Object.is()** method can help you will like checking if the coming number is positive or Negative Zero and so On.

4. Same Value Zero Equality Algorithm

Algorithm’s Name explains itself, this algorithm is identical to the previous one except that +0 and -0 or **NaN **values are considered the same, So let us implement it:

 function sameValueZero(x, y) {
      if (typeof x === "number" && typeof y === "number") {
        // x and y are equal (may be -0 and 0) or they are both NaN
        return x === y || (x !== x && y !== y);
      }
      return x === y;
    }
Enter fullscreen mode Exit fullscreen mode

Conclusion

To summarise the article. You shouldn’t use loosely equality instead of it you can use strict equality.

You can use the same value or same zero value algorithms in case you need to do specific tasks like checking if the accepted value is +0 or -0 zero …etc

I hope you enjoyed this article and benefited even by 1 percent finally see you in the next article…👋

Top comments (0)