Saturday, November 16, 2024
Google search engine
HomeLanguagesJavascriptInequality(!=) Comparison Operator in JavaScript

Inequality(!=) Comparison Operator in JavaScript

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.

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments