Equality Comparisons in JavaScript

(=) (==) (===)

·

3 min read

There are 3 different value-comparison operations in JavaScript.

  • = single equals.

  • == double equals or loose equality.

  • === triple equals or strict equality.

(=) in JavaScript

(=) is an assignment operator, which sets the variable on the left of the (=) to the value of the expression that is on its right. For example –

  a = 10            // it is fine

but in below example-
10   =  10 
‘a’  =  10 
‘a’  =  ‘a’
Will give Reference Error

We use it to assigns the value of one operand to another. for eg. a = b assigns the value of b to a.

let a=2
let b=3
let c= a+b
//console.log(c) will give 5

(==) in JavaScript

  • Double equals (==) is a comparison operator, which transforms the operands having the same type before comparison.

  • So, when we compare string with a number, JavaScript converts string to a number. An empty string is always converts to zero.

  • A string with no numeric value is converts to NaN (Not a Number), which returns false.

  • (==) operator is an equality operator. It checks whether its two operands are the same or not by changing expression from one data type to others.

  • We can use == operator in order to compare the identity of two operands even though, they are not of a similar type.

  • It compares two values for equality after converting both values to a common type. After conversions, the final equality comparison is performed exactly as === performs it.

  • It is symmetric: A == B always has identical semantics to B == A for any values of A and B.

  • undefined and null are loosely equal;

    undefined == null     true
    null == undefined     true
    
console.log(1 == 1);                      true

console.log('hello' == 'hello');          true

console.log('1' ==  1);                   true

console.log(0 == false);                  true

"1" ==  1;             true
1 == "1";              true
0 == false;            true
0 == null;             false
0 == undefined;        false
null == undefined;     true

=== in JavaScript

=== (Triple equals) is a strict equality comparison operator in JavaScript, which returns false for the values which are not of a similar type. This operator performs type casting for equality. If we compare 2 with “2” using ===, then it will return a false value.

Example of ===

console.log(1 === 1);               true

console.log('hello' === 'hello');   true

console.log('1' ===  1);            false

console.log(0 === false);           false

console.log("3" === 3);             false

console.log(true === 1);            false

console.log(null === undefined);     false

# Conclusion

  • = is used for assigning values to a variable.

  • = is called as assignment operator.

  • = does not return true or false.

  • == is used for comparing two variables, but it ignores the datatype of variable.
  • == is called as comparison operator whereas It is also called as comparison operator.
  • == return true only if the two operands are equal while
  • === is used for comparing two variables, but this operator also checks datatype and compares two values.
  • === returns true only if both values and data types are the same for the two variables.