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

Calculate the length of an associative array using JavaScript


Associative arrays are regular arrays in JavaScript where an element is present at a specific index.

What are the Different Data Types in JavaScript


A web scripting language is called JavaScript. It contains its own data types, much like any other computer language. The type of data that a variable...

JavaScript Immediately-invoked Function Expressions (IIFE)


Learn about Immediately-invoked Function Expressions (IIFE) in JavaScript. Understand their benefits and how to use them effectively. Start coding now

4 Ways to Empty an Array in JavaScript


It is possible to empty an array in a few different ways, so let's go over every method that is available.

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...