Inequality(!=) Comparison Operator in JavaScript
Last Updated :
23 May, 2023
JavaScript Inequality operator is used to compare two operands and returns true if both the operands are unequal it is basically the opposite of the Equality Operator. It is also called a loose inequality check as the operator performs a type conversion when comparing the elements. Also in the case of object comparison, it only compares the reference of the objects.
Syntax:
a != b
Example 1: In this example, we will use the inequality operator on the same data types.
Javascript
let a = 1;
let b = 2;
let c = new String( "Hello" );
let d = new String( "Hello" );
let e = "Hello" ;
console.log(a!=b);
console.log(c!=d);
console.log(c!=e);
|
Output: Strings c and d are the same but when created using a constructor they are treated as objects and hence c != d operation returns true as their reference is different but when compared with String d type conversion takes place to string and false is returned.
true
true
false
Example 2: In this example, we will use the inequality operator on different data types.
Javascript
let a = 1;
let b = "1" ;
let c = true
console.log(a!=b);
console.log(b!=c);
console.log(a!=c);
|
Output: Type conversion takes place and all the values are found equal so false is returned.
false
false
false
Example 3: In this example, we will compare objects using the inequality operator.
Javascript
let a = {
name: "Ram" ,
}
let b = {
name: "Ram" ,
}
console.log(a!=b)
|
Output: Even though both objects contain the same value but their reference is different so true is returned.
true
Supported Browsers:
- Chrome
- Edge
- Firefox
- Opera
- Safari
We have a complete list of JavaScript Comparison Operators, to check those please go through, the JavaScript Comparison Operator article.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...