How To Check If Two Strings Are Not Equal In JavaScript

Many built-in solutions can help you check if two strings are not equal in JavaScript. Get to know them with the examples below.

Check If Two Strings Are Not Equal In JavaScript

With Comparison Operators

The inequality operator (!=) can check two string operands to see if they are not equal. It returns true if this is the case and false otherwise. Its negation is the equality operator, which can produce the same result if you use them correctly:

!(s1 == s2)

s1 != s2

The following example shows how you can implement those operators to check whether two strings are equal:

console.log('ITTutoria' != 'ITTutoria.net');
console.log(!('ITTutoria' == 'ITTutoria.net'));
s = 'ITTutoria';
console.log('ITTutoria' != s);
console.log(!('ITTutoria' == s));

Output:

true
true
false
false

Both of those operators use IsLooselyEqual, one of four JavaScript equality algorithms. If you have two strings, their characters must be the same and in the same order.

Keep in mind that since this is a loose comparison, those operators will convert string objects to primitives when compared to primitives.

After the conversion to primitives, the comparison goes ahead like usual if those primitives are of the same type.

Example:

const s1 = new String("ITTutoria");
const s2 = "ITTutoria";
console.log(s1 != s2);
console.log(!(s1 == s2));
console.log(1 != '1');

Output:

false
false
false

In the above example, s1 is a String object, and s2 is a string primitive. The inequality converts the object into a primitive before carrying out the comparison.

This implicit conversion even applies to number values when you compare them to strings that contain numbers. It works in the same manner as the Number() constructor. The resulting value will be NaN if this conversion fails, leading to the true value of the inequality check. Read this guide if you want to learn more about checking numbers in JavaScript.

In addition to loose inequality, JavaScript also has strict algorithms to compare strings with the strict inequality (!==) and strict equality (===) operators. They don’t implicitly convert operands of different types before the comparison. Those operands are considered not equal right away.

This algorithm always works by looking at the types of the operands first. If those types are different, it considers the value unequal. If the types are the same, it checks the values themselves next. Only when operands also have the same value are they considered equal.

You can see this difference in action with this example:

const s1 = new String("ITTutoria");
const s2 = "ITTutoria";
console.log(s1 !== s2);
console.log(!(s1 === s2));
console.log(1 !== '1');

Output:

true
true
true

Since the operands of all those operators belong to different types, the strict inequality and strict equality operators consider them different, even if they represent the same string when converted to primitives.

Choose the equality algorithm depending on how you plan to make the comparisons. Go for strict equality if you are about the type of your strings, and loose equality otherwise. The results of strict equality are more predictable, making it more recommended in most cases.

Note that all string objects and primitives are loosely unequal to null and undefined according to ECMAScript. But in most JavaScript implementations, this is the case only when the other object also emulates undefined.

With localeCompare()

You can also use the localeCompare() method to verify the inequality of two strings. It is originally designed to check whether a string comes after or before another in alphabetical order. The method returns 0 when those strings are equivalent.

Example:

const s1 = "ITTutoria";
const s2 = "ITTutoria.net";
console.log(s1.localeCompare(s2));
console.log(s1.localeCompare("ITTutoria"));

Output:

-1
0

The -1 value indicates that “ITTutoria” occurs before “ITTutoria.net”, while 0 means the s1 string and “ITTutoria” are equal.

Summary

The comparison operators and localeCompare() method can be used to check if two strings are not equal in JavaScript. Make sure you understand the difference between loose and strict inequality to pick the right solution.

Reference Sources:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Inequality

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Strict_inequality

Leave a Reply

Your email address will not be published. Required fields are marked *