Are wondering how to use not equal in javascript? Confused about the proper symbol to be used in your code?
Perfectly, this article is for you! Keep on reading since…
At the end of this article, you will be able to learn and understand what is not equal in Javascript, you will explore the following:
- How to check not equal in JavaScript?
- Example Programs Using not equal operator (!=)
- Example Programs Using strict not equal operator (!==)
- JavaScript Comparison Operators
Now that we know the objectives of this post, let’s get started!
What is not equal in javascript?
In JavaScript, when we want to check if two things are not equal, we can use the “not equal” operator. This operator is represented by either “!==” or “!=”.
Now, there are two versions of this operator: strict and non-strict inequality.
The strict inequality operator, “!==”, compares not only the values of the operands but also their types. If the values are different or the types are different, it will return true.
On the other hand, the non-strict inequality operator, “!=”, only looks at the values of the operands. It doesn’t pay attention to their types. If the values are not equal, regardless of their types, it will return true.
Let’s see an example to understand it better:
var num1 = 5;
var num2 = "5";
console.log(num1 !== num2); // true (strict inequality - different types)
console.log(num1 != num2); // false (non-strict inequality - equal values)
In this example, num1 is a number, and num2 is a string. Using the strict inequality operator, “!==”, we compare their values and types, which results in true because they are different in both aspects.
However, when we use the non-strict inequality operator, “!=”, it only compares the values, so it returns false since the values are equal, even though the types are not.
So, depending on whether you want to consider both value and type or just the value, you can choose between the strict or non-strict inequality operators.
How to check not equal in JavaScript?
In JavaScript, you can check for inequality between two values using the “not equal” operator (!=) or the “strict not equal” operator (!==). Here’s how you can use each of them:
- Not Equal Operator (!=):
The “not equal” operator compares two values and returns true if they are not equal. It performs type coercion, which means it tries to convert the values to a common type before comparing them.
Example:
Output: - Strict Not Equal Operator (
!==
):The “strict not equal” operator also compares two values for inequality but without performing type coercion. It checks both the values and their types to determine if they are not equal.
Example:
Output: Both operators can be used to compare various types of values such as numbers, strings, booleans, and objects. It’s important to choose the appropriate operator based on your specific comparison needs.
Example Programs Using not equal operator (!=)
Here are some examples of JavaScript programs using the “not equal” operator (!=
):
Example 1: Comparing numbers
const sampleNum1 = 44;
const sampleNum2 = 24;
if (sampleNum1 != sampleNum2) {
console.log("The numbers are not equal.");
} else {
console.log("The numbers are equal.");
}
Output:
The numbers are not equal.
Example 2: Comparing strings
const sampleString1 = "apple 🍎";
const sampleString2 = "banana 🍌";
if (sampleString1 != sampleString2) {
console.log("The strings are not equal.");
} else {
console.log("The strings are equal.");
}
Output:
The strings are not equal.
Example 3: Comparing different data types
const number = 10;
const string = "strawberry 🍓";
if (number != string) {
console.log("The values are not equal.");
} else {
console.log("The values are equal.");
}
Output:
The values are not equal.
Example 4: Comparing boolean values
const sampleBool1 = true;
const sampleBool2 = false;
if (sampleBool1 != sampleBool2) {
console.log("The boolean values are not equal.");
} else {
console.log("The boolean values are equal.");
}
Output:
The boolean values are not equal.
These examples demonstrate the usage of the “not equal” operator in JavaScript to compare different types of values and determine whether they are equal or not.
Example Programs Using strict not equal operator (!==)
Here are some examples of JavaScript programs using the “strict not equal” operator (!==
):
Example 1: Comparing numbers
const number1 = 55;
const number2 = "55";
if (number1 !== number2) {
console.log("The numbers are not strictly equal.");
} else {
console.log("The numbers are strictly equal.");
}
Output:
The numbers are not strictly equal.
Example 2: Comparing strings
const sampleString1 = "orange 🍊";
const sampleString2 = "Orange 🍊";
if (sampleString1 !== sampleString2) {
console.log("The strings are not strictly equal.");
} else {
console.log("The strings are strictly equal.");
}
Output:
The strings are not strictly equal.
Example 3: Comparing boolean values
const sampleBool1 = true;
const sampleBbool2 = 1;
if (sampleBool1 !== sampleBool2) {
console.log("The boolean values are not strictly equal.");
} else {
console.log("The boolean values are strictly equal.");
}
Output:
The boolean values are not strictly equal.
Example 4: Comparing different data types
const number = 105;
const string = "105";
if (number !== string) {
console.log("The values are not strictly equal.");
} else {
console.log("The values are strictly equal.");
}
Output:
The values are not strictly equal.
These examples demonstrate the usage of the “strict not equal” operator in JavaScript to compare different types of values and determine whether they are strictly equal or not. The strict not equal operator (!==
) checks for both value and type inequality.
JavaScript Comparison Operators
Here are other comparison operators you can utilize and solve different use cases.
Operator | Description |
---|---|
== | Equality (checks for equal value, performs type coercion) |
=== | Strict Equality (checks for equal value and type) |
!= | Inequality (checks for not equal value, performs type coercion) |
!== | Strict Inequality (checks for not equal value or type) |
> | Greater than |
>= | Greater than or equal to |
< | Less than |
<= | Less than or equal to |
In JavaScript, the proper operator for “not equals” is represented by the symbol “!=”.
The “!=” operator checks for non-strict inequality, meaning it compares the values of the operands without considering their types. It returns true if the operands are not equal in value.
It’s important to note that if you want to check for strict inequality, comparing both the value and the type of the operands, you should use the “!==”” operator.
In JavaScript, the “!=” operator is used for non-strict inequality comparison, while the “!==”” operator is used for strict inequality comparison.
To learn more about JavaScript functions here are other resources you can check out:
- How to Replace div Content JavaScript
- Numberformat JavaScript | Formatting Numbers Made Easy
- How to get length of dictionary JavaScript | 3 Methods
Conclusion
In conclusion, this article provides an explanation of the ” not equal” operator in Javascript, which is represented by either “!=” or “!==”.
The strict inequality operator “!==” compares both the value and types of the operands while the non-strict operator “!=” only compares the value.
Further, the given examples and programs demonstrate the usage of both operators for comparing different types of values. In this case, it is important to choose the appropriate operator based on the desired comparison needs.
The article also mentioned, other comparison operators which can be used in various use cases. Overall this article serves as a helpful guide for understanding and using the “not equal” operator in JavaScript.