JavaScript: Comparison Operators

Profile picture for user arilio666

Hey, what is comparison? Y'all know that comparison is the distinction between two things, vary the ups and downs. Likewise, the comparison operator in javascript is the same, which distinguish / compare between the variables or values.

So say if we assign a value to a variable of name x.

1) == (Equal to)

This operator returns the equality of the logical condition of a variable. Let us see it in action with the if-else statement.

x = 100;

if(x == 100){
console.log("It's equal")
}else{
    console.log("Not equal")
}

Output: It's equal

2) != (Not Equal to)

This operator gives us the not equal comparison of the variable we give as the condition.

x = 120;

if(x != 100){
console.log("It's equal")
}else{
    console.log("Not equal")
}

Output: Not equal

3) === (Strict Equal To)

When we use this, the given condition should be strictly equal to the variable value and of the same type.

x = 100;

if(x === '100'){
console.log("It's equal")
}else{
    console.log("Not equal")
}

Output: Not equal

Because we declared x as integer type and not as a string since it has to be equal in type too, here 100 is not equal to '100' 

4) !== (Strict Not Equal To)

This operator is satisfied when the operand / value of the variable is equal, but the type is different or not equal at all.

x = 100;

if(x !== '100'){
console.log("if block executed")
}else{
    console.log("else block executed")
}

Output: if block executed

So here, it doesn't care about the type, meaning that this operator needs at least some match to be satisfied.

5) > (Greater Than)

If the value / operand of x is greater than the assigned value, then the condition is satisfied as true.

x = 100;

if(x > 100){
console.log("value is greater than 100")
}else{
    console.log("value is less than or equal to 100")
}

Output: value is less than or equal to 100

6) >= (Greater Than Or Equal To)

When the operand / value of x is greater or equal to the value assigned, this satisfies.

x = 100;

if(x >= 100){
console.log("value is greater than or equal to 100")
}else{
    console.log("value is less than 100")
}

Output: value is greater than or equal to 100

7) < (Less Than)

Quite the opposite of > this operator is satisfied when the assigned value is less or it'' returns false.

x = 100;

if(x < 100){
console.log("value is less than 100")
}else{
    console.log("value is greater than or equal to 100")
}

Output: value is greater than or equal to 100

8) <= (Less Than Or Equal To)

If the operand/value is lesser or equal to the assigned value, this satisfies.

x = 100;

if(x <=100 ){
console.log("value is less than or equal to 100")
}else{
    console.log("value is greater than 100")
}

Output: value is less than or equal to 100