Difference between != and !== operator in JavaScript

  • != operator
    • The inequality operator (!=) is the logical opposite of the equality operator. It means “Not Equal” and returns true where equality would return false and vice versa.
    • Like the equality operator, the inequality operator will convert data types of values while comparing.
    • For example 1 != ‘1’ will return false since data type conversion take place so 1 and ‘1’ are considered equal.
  • !== operator
    • The strict inequality operator (!==) is the logical opposite of the strict equality operator. It means “Strictly Not Equal” and returns true where strict equality would return false and vice versa.
    • Strict inequality will not convert data types.
    • For example 1 !== ‘1’ will return true since 1 is an integer and ‘1’ is a character and no data type conversion take place.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
    function NotEqual(val) {
        if (val != 1) {
            return "Both are Not Equal i.e. condition is true";
        } else {
            return "Both are Equal i.e. condition is false";
        }
    }
  
function StrictlyNotEqual(val) {
    if (val !== 1) {
        return "Both are Not Equal i.e. condition is true";
    } else {
        return "Both are Equal i.e. condition is false";
    }
}
  
//Passing integer
  
console.log("  1 != 1 " + NotEqual(1));
console.log("  1 !== 1 " + StrictlyNotEqual(1));
  
//Passing character
  
console.log(" '1' != 1 " + NotEqual('1'));
console.log(" '1' !== 1 " + StrictlyNotEqual('1'));
  
//Passing string
  
console.log(" \"1\" != 1 " + NotEqual("1"));
console.log(" \"1\" !== 1 " + StrictlyNotEqual("1"));
  
//Passing false value i.e. 0
  
console.log("  0 != 1 " + NotEqual(0));
console.log("  0 !== 1 " + StrictlyNotEqual(0)); 
</script>

chevron_right


Output:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.