Comparison Operators


JavaScript comparison operators are fundamental for decision-making in your code. They compare two values and return a Boolean result: true or false. Understanding the difference between loose equality (==) and strict equality (===) is crucial for writing predictable and bug-free code. The golden rule is to always default to using strict equality (===) to avoid unexpected type coercion issues.


Loose Equality (==) and Loose Inequality (!=)

The loose equality operator (==) compares two values for equality after attempting to convert them to a common type. This process, called type coercion, can sometimes lead to unintuitive results. The loose inequality operator (!=) works similarly, returning true if the values are not equal after type coercion.

Example 1: Loose Equality (==)

// Example of loose equality in JavaScript
let numberValue = 10;
let stringValue = '10';

// The '==' operator converts the string '10' to a number before comparing.
console.log(numberValue == stringValue); // Outputs: true

Explanation

In this example, numberValue is a number and stringValue is a string. The == operator performs type coercion, converting the string '10' into the number 10 before making the comparison. Because 10 is equal to 10, the expression evaluates to true.


Example 2: Loose Equality with Different Types

// Example demonstrating type coercion with loose equality
let zero = 0;
let falseBoolean = false;

// The '==' operator coerces the boolean 'false' to the number 0.
console.log(zero == falseBoolean); // Outputs: true

Explanation

Here, the boolean value false is coerced into the number 0 by the == operator. Since 0 is equal to 0, the result is true. This implicit conversion can sometimes hide bugs in your logic if you're not careful.


Example 3: Loose Inequality (!=)

// Example of loose inequality in JavaScript
let valueA = 5;
let valueB = '6';

// The '!=' operator checks for inequality after type coercion.
console.log(valueA != valueB); // Outputs: true

Explanation

The loose inequality operator != first coerces the values to a common type. The string '6' becomes the number 6. Since 5 is not equal to 6, the expression correctly returns true.


Strict Equality (===) and Strict Inequality (!==)

Strict equality (===) is known as the identity operator. It compares two values for equality without performing any type conversion; if the types are different, it immediately returns false. This makes your code more predictable and is the recommended best practice. The strict inequality operator (!==) returns true if the values are not equal or their types are not the same.

Example 1: Strict Equality (===)

// Example of strict equality in JavaScript
let numberValue = 25;
let stringValue = '25';

// The '===' operator checks both value and type. Since types are different, it's false.
console.log(numberValue === stringValue); // Outputs: false

Explanation

Unlike loose equality, the strict equality === operator checks for both value and type. In this case, numberValue is a number and stringValue is a string. Because their types are different, the comparison results in false without any type coercion.


Example 2: Strict Equality with Same Type and Value

// Example where strict equality returns true
let num1 = 50;
let num2 = 50;

// Both value and type are the same (Number), so the result is true.
console.log(num1 === num2); // Outputs: true

Explanation

This example shows a scenario where === evaluates to true. Both num1 and num2 have the same value (50) and the same type (number). Therefore, the strict equality operator returns true.


Example 3: Strict Inequality (!==)

// Example of strict inequality in JavaScript
let val1 = 100;
let val2 = '100';

// The '!==' operator returns true because the types are different.
console.log(val1 !== val2); // Outputs: true

Explanation

The strict inequality operator !== returns true if the operands are of different types or have different values. Here, val1 is a number and val2 is a string. Because the types do not match, the expression evaluates to true.


Relational Operators (>, <, >=, <=)

Relational operators are used to compare the magnitude of two values. These include greater than (>), less than (<), greater than or equal to (>=), and less than or equal to (<=). When comparing values of different types, JavaScript will still attempt to convert them to numbers.

Example 1: Greater Than (>) and Less Than (<)

// Example of greater than and less than operators
let score = 88;
let passingScore = 75;

// These operators compare numerical values.
console.log(score > passingScore); // Outputs: true
console.log(score < passingScore); // Outputs: false

Explanation

In this code, we are comparing the score to the passingScore. Since 88 is greater than 75, the > comparison returns true. The < comparison returns false because 88 is not less than 75.


Example 2: Greater Than or Equal To (>=)

// Example using the greater than or equal to operator
let playerAge = 18;
let requiredAge = 18;

// '>=' checks if the value is greater than or equal to the other.
console.log(playerAge >= requiredAge); // Outputs: true

Explanation

The >= operator checks if the left operand is greater than or equal to the right operand. Since playerAge (18) is equal to requiredAge (18), the condition is met, and the expression evaluates to true.


Example 3: Less Than or Equal To (&lt;=) with Type Coercion

// Example of less than or equal to with different types
let inventoryCount = '50';
let maxInventory = 100;

// The string '50' is coerced to a number before the comparison.
console.log(inventoryCount <= maxInventory); // Outputs: true

Explanation

This example demonstrates type coercion with a relational operator. The inventoryCount is a string ('50'), which JavaScript converts to a number (50) before comparing it to maxInventory (100). Since 50 is less than or equal to 100, the result is true.