JavaScript provides two comparison operators, namely '==' and '==='. Both are used to compare two values, but they work differently.
Double Equals (==) Operator
It is also known as loose equality operator. It compares two operands for equality after converting them to a common type.
For Example - If the two operands are of different types, then the JavaScript engine will convert one of the operands to the same type as the other operand.
10 == '10' // output=========> true
//Reason - because the number 5 is converted to the string 5
Triple Equals (===) Operator
It is also known as strict equality operator. It compares two operands for equality without converting them to a common type.
For Example - If the two operands are of different types, then it will return false.
10 == '10' // output=========> false
//Reason - because both the operands are of different data types
It is generally recommended to use the triple equal operator whenever possible to avoid unexpected behavior due to type coercion.
Thank you for reading!!
Top comments (0)