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:
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