Skip to content

How to compare two strings in JavaScript if condition not equal | Example

  • by

Use strict inequality operator (!==) to compare two strings in JavaScript if conditions are not equal in JavaScript. The strict inequality operator always considers operands of different types to be different.

Compare two strings in JavaScript if the condition is not equal

Simple example code using if statement with not equality operator in JavaScript.

<!DOCTYPE html>
<html>
<body>

  <script>
    var compare = "page2"
    if (compare !== "page1"){
      console.log("It's not", compare)
    }
  </script>

</body>
</html> 

Output:

How to compare two strings in JavaScript if condition not equal

Note: the “not equal” operator (!=) performs a type coercion if the operands are of different types. To avoid this, you can use the strict not equal operator (!==) which also checks the types of the operands.

Do comment if you have any doubts or suggestions on this JS string topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading