DEV Community

Cover image for Javascript Equality comparison Operator ( "==") or ("===")
Mehammed Teshome
Mehammed Teshome

Posted on

Javascript Equality comparison Operator ( "==") or ("===")

Comparison operators compares two values and return a boolean value. equality operators are also comparison operator which checks the equality of values and returns boolean.

in javascript(ES6), There are four(4) which are listed below.

  • Using ‘==’ operator
  • Using ‘===’ operator
  • SameValueZero: used mainly in sets, maps and arrays.
  • SameValue: used elsewhere

which operation you choose depend on the type of comparison you are looking to perform.

  • double equals (==) will perform a type conversion when comparing two things, and will handle NaN, -0, and +0 specially to conform to IEEE 754 (so NaN != NaN, and -0 == +0);
  • triple equals (===) will do the same comparison as double equals (including the special handling for NaN, -0, and +0) but without type conversion; if the types differ, false is returned.
  • Object.is does no type conversion and no special handling for NaN, -0, and +0 (giving it the same behavior as === except on those special numeric values).

Strict equality using ('===')

  • strict equality compares two values for equality .
  • neither values is implicitly converted to some other value before beign compared.
  • if values have different types , the values are considered unequal. if the value have the same type , are not numbers and have the same value, they are cosidered equal.
  • if both values are numbers, they are considere equal if they are both not NaN and are same value, or if one is +0 and one is -0.
  • a values is only equal to itself for all values except number.
  • for numbers it is +0===-0. is true
  • the only case in which (x!==x) is true is when x is NaN. strictly equal example

Loose Equality using (" == ")

  • loos equality compares two values for equality after converting both values to a common type(one or both sides may undergo conversion).
  • after conversion , the final equality comparison is performed exactly as === performs it.
  • A==B always has identical semantics to (B==A) for any values of A and B .
  • undefined and nurll are loosely equal; that is , ( undefined == null) is true. and
  • (null == undefined ) is also true. example below

loosely equal example

Same-value Equality

  • it checks weather two values are functionally identical in all contexts.
  • one instance occurs when an attempt to mutate un immutable object property is made.
  • this equality is provided with (object.defineProperty())and (Object.is()) methods.

Top comments (0)