JavaScript Tutorialprovides basic and advanced concepts of C# for beginners and professionals.

Difference between == and === in JavaScript

Back to: JavaScript Tutorial

In JavaScript, == and === are both comparison operators, but they behave differently:

  1. == (Loose Equality or Abstract Equality):

    • Compares two values for equality, but with type coercion.
    • This means that if the values are of different types, JavaScript will try to convert them to the same type before making the comparison.

    Example:

    
        
    javascript
    5 == '5' // true because '5' is coerced to a number null == undefined // true because they are considered equal when using ==
    
        
  2. === (Strict Equality):

    • Compares both value and type without any type coercion.
    • The comparison will return true only if both the value and the type are the same.

    Example:

    
        
    javascript
    5 === '5' // false because one is a number and the other is a string null === undefined // false because they are different types
    
        

In general, it's recommended to use === (strict equality) to avoid unexpected behavior caused by type coercion.

Scroll to Top