Menu
Posted At Thu, Jan 18, 2024 11:46 AM

JavaScript Value Comparison Operators

Listen | 3 min read

Table of Contents

  1. Introduction
  2. Equality Operator (==)
  3. Strict Equality Operator (===)
  4. Inequality Operator (!=)
  5. Strict Inequality Operator (!==)
  6. Greater Than Operator (>)
  7. Less Than Operator (<)
  8. Greater Than or Equal Operator (>=)
  9. Less Than or Equal Operator (<=)
  10. Conclusion

1. Introduction <a name="introduction"></a>

JavaScript provides various comparison operators that allow developers to compare values and make decisions based on the results. These operators return a Boolean value (true or false) based on whether the comparison is true or false. In this article, we will explore different JavaScript value comparison operators and their usage.

2. Equality Operator (==) <a name="equality-operator"></a>

The equality operator (==) checks if two values are equal and performs type coercion if necessary. It converts the operands to the same type before making the comparison.

let num1 = 5;
let num2 = '5';

console.log(num1 == num2); // true (after type coercion)

3. Strict Equality Operator (===) <a name="strict-equality-operator"></a>

The strict equality operator (===) also checks if two values are equal but does not perform type coercion. Both the value and the type of the operands must be the same for the comparison to be true.

let num1 = 5;
let num2 = '5';

console.log(num1 === num2); // false (no type coercion)

4. Inequality Operator (!=) <a name="inequality-operator"></a>

The inequality operator (!=) checks if two values are not equal and performs type coercion if necessary.

let num1 = 5;
let num2 = '5';

console.log(num1 != num2); // false (after type coercion)

5. Strict Inequality Operator (!==) <a name="strict-inequality-operator"></a>

The strict inequality operator (!==) checks if two values are not equal and does not perform type coercion.

let num1 = 5;
let num2 = '5';

console.log(num1 !== num2); // true (no type coercion)

6. Greater Than Operator (>) <a name="greater-than-operator"></a>

The greater than operator (>) checks if the left operand is greater than the right operand.

let num1 = 10;
let num2 = 5;

console.log(num1 > num2); // true

7. Less Than Operator (<) <a name="less-than-operator"></a>

The less than operator (<) checks if the left operand is less than the right operand.

let num1 = 5;
let num2 = 10;

console.log(num1 < num2); // true

8. Greater Than or Equal Operator (>=) <a name="greater-than-or-equal-operator"></a>

The greater than or equal operator (>=) checks if the left operand is greater than or equal to the right operand.

let num1 = 10;
let num2 = 10;

console.log(num1 >= num2); // true

9. Less Than or Equal Operator (<=) <a name="less-than-or-equal-operator"></a>

The less than or equal operator (<=) checks if the left operand is less than or equal to the right operand.

let num1 = 5;
let num2 = 5;

console.log(num1 <= num2); // true

10. Conclusion <a name="conclusion"></a>

JavaScript value comparison operators are crucial for making decisions and controlling the flow of programs. Understanding the differences between equality operators, inequality operators, and relational operators is essential for writing reliable and efficient code. By using these operators appropriately, developers can create robust and accurate comparisons in their JavaScript applications.

1324 1

Comments