. Advertisement .
..3..
. Advertisement .
..4..
Strings are among the essential components of any programming language, given how frequently text needs to be represented. Are you searching for a way to check if two strings in JavaScript aren’t equal? You are at the right place! This guide with examples will explain how to perform this task. Check it out and follow along!
What Are JavaScript Comparison Operators?
As the name suggests, the comparison operators in JavaScript are applied when comparing two values. These operators give back either false or true as a boolean value depending on the particular condition. As a result, they have employed conditional expressions for loop or decision-making.
Every developer should be aware of each operator’s capabilities, given how widely it is used. In this case, we will focus on the != operator, which can help you determine whether two strings in JavaScript aren’t equal.
Check If Two Strings In JavaScript Aren’t Equal Utilizing !==
Utilize the strict inequality operator !== to determine whether two strings are not equal and receive boolean values. If the strings are not equal, the strict inequality operator gives back the true value. On the contrary, if they are equal, the operator gives back the false value.
Here is an example for you to use as a reference.
const a = "Hello";
const b = "World";
if (a !== b) {
console.log("Strings are not equal");
} else {
console.log("Strings are equal");
}
Output:
Strings are not equal
In contrast to the operator ===, ! negates the operator’s result.
Below is another example of the result of the operator !==.
console.log("Hello" !== "World"); // true
console.log("Hello" !== "Hello"); // false
console.log("" !== ""); // false
console.log(null !== null); // false
console.log(undefined !== undefined); // false
console.log(0 !== 0); // false
console.log(1 !== 1); // false
console.log(NaN !== NaN); // true
console.log(Infinity !== Infinity); // false
console.log(true !== true); // false
console.log(false !== false); // false
console.log([] !== []); // true
console.log([1] !== [1]); // true
console.log({} !== {}); // true
Be careful not to mix up !== and !=. In contrast to the loose inequality operator !=, the strict inequality operator !== treats two values of different kinds as distinct. As opposed to strict inequality, which compares values of the same type, loose one tries to convert values of different types to the same one prior to comparison.
Keep in mind that the two values in a strict inequality must be the same type in order for it to be assumed that they are not equal.
The Bottom Line
Above is the method you can use to check if two strings in JavaScript aren’t equal. Hopefully, this guide has cleared out your confusion about this issue. Put this method into practice and see how well it works.Bonus: We also provide several other JavaScript tutorials to help you solve any problem you might encounter, such as how to check if a variable is not null or check if a string is empty. Check out our site now to learn more programming skills!
Leave a comment