In JavaScript, the equality operator is used to determine whether two values are equal. JavaScript's == and === operators are used to compare. The primary distinction between the == and === operators in JavaScript is that the former converts the operands' data types before comparison, whilst the latter compares both the operands' values and their data types.

We use comparison operators in JavaScript to compare two values. Two values can be compared in a special example of comparison to determine whether they are equal (or unequal). In that situation, we can employ the following two JavaScript operators:

  • == operator
  • === operator

Syntax

x == y
x === y

Example

// Double Equals
console.log(2 == "2");  // true
console.log(0 == ""); // true
console.log(null == undefined); // true
console.log([] == ""); // true

// Triple Equals
console.log(2 === "2"); // false
console.log(0 === ""); // false
console.log(null === undefined); // false
console.log([] === ""); // false

Because the "2" is changed to the number 2 before the comparison is made, the abstract equality operator for the expression 2 == "2" yields true.
Due to the various data types, the strict equality operator returns false when 2 === "2."

Conclusion

  • To verify that two operands are equal, use the == and === operators.
  • To verify the inequality of two operands, use the != and !== operators.
  • The equality operators == and!= conduct type conversion on the operands before comparison since they are loose equality operators.
  • Since they compare the operands without any type conversion and return false (in the case of the === operator), the stringent equality operators === and !== compare operands regardless of whether they are of the same data type.
  • The == and != operators can be used to compare two operands when the data types of the operands aren't a significant component in the comparison. For instance, the database's admission numbers can be compared to the student's admission number, which was obtained through a form and may be of the string or numeric type (in number data type).
  • When the data type of the operands is crucial to the comparison and cannot be changed, the === and !== operators are used to make the comparison. For instance, in a coding competition, the response could take either number or string form, but per the rules, only string-type responses will receive points. In this instance, we'll compare the user's responses to the solution kept in our database using the === operator.

Recommended Posts

View All

Difference between var and let in JavaScript


Learn the difference between var and let in JavaScript. Understand variable hoisting, scope, and how they affect your code's behavior. Get started now...

JavaScript Program to Create Objects in Different Ways


Learn different ways to create objects using JavaScript with our step-by-step guide. From object literals to constructor functions and classes, this p...

Useful websites every developer should know


There are numerous websites available nowadays to assist in the creation of a distinctive website.

What is JSON? Know how it Works with Examples


Learn what JSON is and how it works with examples in this comprehensive guide. Discover the basics of JSON syntax, its applications, and why it has be...

JavaScript Template Literals Explained


JavaScript template literals are one of JavaScript's most powerful capabilities. They enable us to generate dynamic strings without the need for conca...