Using
Often programmers face situations when they need to check whether certain values are equal or not. Those are the cases when comparison operators come to the aid. One of them is "not equal".
To point out that two numbers or expressions are not equal mathematicians typically use a crossed equal sign (≠). However, in the world of programming it might be quite inconvenient, so in order to check the inequality it's set to use !=
.
With the help of this sign combination you can give an instruction to compare two values. If they are not the same, the operator will return a boolean result true
, otherwise you will get false
. In most cases this operator is used as a condition in the if
clause, but you can as well assign its result to a created variable. The syntax is pretty easy and is shown below.
a = 5;
b = 6;
result = a!=b;
console.log(result); // logs "true"
if(a!=b) {
console.log("The numbers are not equal");
}
The process of comparison by !=
follows certain rules. First of all, if two values have the same data type, the operator will start the comparison. If the type is different, these values will be converted to the same one first and compared only after that.
If one of the values is null
and the other one is undefined
, they will be considered as equal and !=
will return false
. At the same time these values are not equal to everything else. Even comparing null
and 0
will show that they are not the same.
When one of the values is a string and the other one is a number, the operator will convert the string into a numeric value first and then compare. For example, the result of checking if number 5 and string "5" are not equal would be false
.
a = 5;
b = "5";
result = a!=b;
console.log(result); // logs "false"
In case of comparing boolean value true
, it will be converted into 1
and false
goes to 0
. In this way you can as well compare strings with boolean values. However, pay attention that the string of characters "false" is not equal to the boolean value false
, when string "0" is equal to it.
console.log(false!="false"); // logs "true", they are not equal
console.log(false!="0"); // logs "false", they are equal
If one of the values is an object, first of all the operator will run the valueOf()
method to convert it into a primitive data type, the second method to use will be toString()
. After converting the comparison will take place.
The operator of strict inequality
Sometimes the fact that the operator of inequality automatically converts the values of different data types might be disadvantageous. For instance, it does not see the difference between 0
and false
or even between an empty string and false
. For such situations there is an operator of strict inequality marked as !==
.
This operator will return true
if the values are not equal or if their data types are different. In other words if you try to check whether 0
and false
are not equal, the operator will immediately return true
since the data types are not the same.
a = 0;
b = false;
console.log(a!==b); // logs "true"
console.log(a!=b); // logs "false"
Note, that even two NaN
(not-a-value) variables are considered as different by the operator of strict inequality. At the same time if both of them are null
or undefined
, they will be taken as the same and !==
will return false
.
console.log(NaN !== NaN); // logs "true"
console.log(null !== null); // logs "false"
console.log(undefined !== undefined); // logs "false"
console.log(null !== undefined); // logs "true"
Even though the strict inequality operator is one sign longer to write, using it is more recommended since it leaves you less chance to face a mistake in your program.
Using the inequality operator with arrays
Although working with this operator might seem pretty easy, there are some features that might confuse you from the first sight. For example, let's compare two arrays containing the same elements.
array1 = [1, 2, 3];
array2 = [1, 2, 3];
console.log(array1!=array2); // logs "true"
console.log(array1!==array2); // logs "true"
From this example you can see that even though two arrays seem to be equal, they have the same length and consist of the same elements, both operators of inequality consider them different and return true
.
It happens because the arrays in JavaScript are objects first of all and as well as with any other objects these operators compare their references instead of their actual values. Since the instances are not the same, then two objects are different as well. That's why a created array will be equal to itself only.
For some simple cases an alternate way to compare arrays is to convert them into strings first. From the code below you can see that both inequality operators work well with string representations of the created arrays.
array1 = [1, 2, 3];
array2 = [1, 2, 3];
console.log(array1.toString()!=array2.toString()); // logs "false"
console.log(array1.toString()!==array2.toString()); // logs "false"
Using this solution for comparing custom objects is not recommended though. Although it may seem to work and return a proper result on the first sight, going further you may face some very unpleasant bugs. That's why for more complicated comparisons it's always better to create your own function for checking inequality.
To sum up, JavaScript offers simple syntax for checking whether two values are not equal. Using !=
or !==
gives you an opportunity to specify if the data type is important for you or it can be neglected. However, these operators cannot deal with complicated objects, so you might need to convert them into strings before comparison or even create your own function for checking non-equality.