Understanding Not Equal and Other Comparison Operators in JavaScript
Written on
Chapter 1: The Not Equal Operator
In programming, comparison operators are essential tools that allow us to evaluate expressions, returning either true or false. Among these, the "not equal" operator is particularly useful in determining if two values differ. This operator yields true when its operands are not equal and false when they are the same.
The JavaScript not equal operator, also known as the "inequality" operator, is represented by !=. This symbol combines an exclamation mark (indicating "not") and an equals sign. Essentially, it enables the comparison of two values irrespective of their type. There's also a variant, !==, referred to as the strict inequality operator, which checks both value and type, returning false if the operands are of different types.
Syntax
The syntax for using the not equal operator is straightforward:
x != y
Here, x and y represent the operands being compared. The inequality operator allows you to determine their equality.
Examples and Explanations
Consider the following examples:
console.log(9 != 12);
console.log(12 != 12);
console.log(15 != '15');
console.log(13 != '13');
console.log('hello' != 'hello');
console.log('hello' != 'HELLO');
console.log(13 !== '13');
Results:
true
false
false
false
false
true
false
In the first example, the values 9 and 12 are not the same, resulting in a true output. Conversely, since both operands in the second example are identical (12 and 12), the output is false. In the third and fourth examples, although we compare a number to a string, the not-equal operator treats them as equal, hence returning false.
The fifth example showcases two identical strings, yielding a false result. In the sixth case, case sensitivity comes into play, as the different cases of "hello" yield a true result. Finally, the seventh example uses the strict inequality operator, which returns false because the operands differ in type.
Other Comparison Operators
So far, we have examined the not equal and strict inequality operators. Below are additional comparison operators commonly used in JavaScript:
- Equal to (==): Checks if two values are equivalent.
- Strict equal to (===): Confirms both value and type are identical.
- Less than (<): Evaluates if the left operand is smaller than the right.
- Less than or equal to (<=): Assesses if the left operand is smaller than or equal to the right.
- Greater than (>): Determines if the left operand is larger than the right.
- Greater than or equal to (>=): Checks if the left operand is larger than or equal to the right.
Examples of Other Comparison Operators
console.log(80 == 100);
console.log(40 === 40);
console.log(40 === '40');
console.log(10 < 20);
console.log(10 <= 10);
console.log(50 > 10);
console.log(50 >= 50);
Results:
false
true
false
true
true
true
true
The above examples illustrate how these operators function. It's essential to note the strict equality operator in the second example, where both operands possess the same type, returning true. In the third example, differing types lead to a false outcome.
Summary
To summarize, the not equal operator is sensitive to case and distinguishes between != and !==. The strict inequality operator is particularly strict about operand types, which is crucial for accurate comparisons. Remember, the not equal operator returns true when the operands differ and false when they are the same.
Do You Want to Fast Track Your Career as a Programmer?
Join a community of like-minded individuals who are passionate about programming and technology. Here, we tackle the significant challenges faced by programmers and engage in discussions about both frontend and backend engineering. Together, we can enhance your understanding of various technological concepts.
This video titled "18 JS Comparison Operators | JavaScript Full Tutorial" provides a comprehensive overview of comparison operators in JavaScript, making it a great resource for anyone looking to deepen their understanding of the subject.
The second video, "JavaScript Tutorial For Beginners #14 - Comparison Operators," is tailored for beginners, offering clear explanations and examples that enhance foundational knowledge in JavaScript comparison operators.