Equality operators in JavaScript

Equality operators in JavaScript

Equality operators :

There are two operators in javascript for determining the equality of two operands. This is essentially used to compare two operands and return a boolean value. The operators are known as loose equality (==) or double equals (==) and strict equality (===) or triple equals(===). Both are used for comparison, but they behave differently for different purposes. In this blog post, we will learn more about these operators and their applications.

Loose equality operators (==) :

The "loose equality" operator is also known as the ==operator. After converting both values to a common type, it evaluates two values for equality. Type coercion is another name for this behavior. JavaScript will attempt to convert the values before comparing them when they are not of the same type. In some cases, this can produce unexpected results.

For example, let's consider the following code :

console.log(10 == "10"); // true

In the example above, the loose equality (==) operator is used to determine whether two different datatypes, such as a string and a number, that have the same value, "10," are equal. Due to the different datatypes of the two operands, JavaScript will convert them into a common time before comparison. As a result of turning the string "10" into the number 10, it returns true in this example.

The followings are some use cases of == operators such as

  1. Converting values to a common type by force

     const num= 10;
     const str= "10";
     console.log(num == str); // true
    
  2. While comparing values of different types

     console.log(1=="1") //true
    
  3. Determining whether a value is null or undefined

     let a;
     console.log(a==null);//true 
     console.log(a==undefined);//true
    

Strict equality operators(===) :

Another name for the "strict equality" operator is the triple-equals operator. The triple-equals operator (===) is used in a situation where we need to perform a strict equality comparison. It only compares the values and their types when both are the same, returning true. Unlike the double equals operator, the triple equals operator does not use type coercion.

For example, let's consider the following code :

console.log(10 === "10"); // false

The triple equals operator is used in this example to compare the number 10 to the string "10". Because the two operands are of different types and the operator is strict, even though the values are equal, the comparison returns false.

The followings are some use cases of === operators such as

  1. To avoid type coercion while comparing two different datatypes

     console.log("0" === false); // false
    
  2. Comparing two boolean values such as 0 with false or 1 with true

     const a = true;
     const b = 1;
     console.log(a === b); // false
    
  3. To check if both operands are identical in terms of values and types

     var x = 7;
     var y = "7";
     console.log(x === y); // false
    

Conclusion :

The == and === operators in JavaScript are used to compare two values for equality. The == operator is known as the "loose equality" operator because it performs type conversion before comparing values, whereas the === operator is known as the "strict equality" operator because it does not perform type conversion. To avoid unexpected results, use the === operator for all equality comparisons in JavaScript.