JS Comparisons

Overview

Comparison operators in JavaScript are used to compare two values and return a Boolean result based on the comparison. They are fundamental for making decisions and controlling the flow of execution in programs.


Equal to (==)

The == operator checks if two values are equal, performing type coercion if needed.

Try yourself
        
            
let x = 5;
let y = 5;
let isEqual = x == y;         // true

        
    

Not Equal to (!=)

The != operator checks if two values are not equal, performing type coercion if needed.

Try yourself
        
            
let x = 5;
let y = 10;
let isNotEqual = x != y;      // true

        
    

Strict Equal to (===)

The === operator checks if two values are strictly equal, meaning they have the same value and type.

Try yourself
        
            
let x = 5;
let y = '5';
let isStrictEqual = x === y;  // false

        
    

Strict Not Equal to (!==)

The !== operator checks if two values are strictly not equal, meaning they have either different values or different types.

Try yourself
        
            
let x = 5;
let y = '5';
let isStrictNotEqual = x !== y; // true

        
    

Greater than (>)

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

Try yourself
        
            
let x = 5;
let y = 10;
let isGreater = x > y;        // false

        
    

Less than (<)

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

Try yourself
        
            
let x = 5;
let y = 10;
let isLess = x < y;           // true

        
    

Greater than or Equal to (>=)

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

Try yourself
        
            
let x = 5;
let y = 5;
let isGreaterOrEqual = x >= y; // true

        
    

Less than or Equal to (<=)

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

Try yourself
        
            
let x = 5;
let y = 10;
let isLessOrEqual = x <= y;   // true

        
    

Summary

Comparison operators in JavaScript are used to compare two values and return a Boolean result. These operators are essential for making decisions and controlling the flow of execution in programs. Understanding how each operator works is crucial for writing effective JavaScript code.