Home Basics JavaScript comparison operators

JavaScript comparison operators

The JavaScript comparison operator compares the two operands.

The following table illustrates the JavaScript comparison operators:

Operator Description Example
== Is equal to 10==20 = false
=== Identical (equal and of same type) 10==20 = false
!= Not equal to 10!=20 = true
!== Not Identical 20!==20 = false
> Greater than 20>10 = true
>= Greater than or equal to 20>=10 = true
< Less than 20<10 = false
<= Less than or equal to 20<=10 = false

Some examples

== operator

let a = 10, 
    b = 20;
    c = 10;	
console.log(a == 10); // true
console.log(b == 10); // false

=== operator

let a = 10, 
    b = 20;
    c = 10;	
console.log(a === 10); // true
console.log(a === b); // false
console.log(a === c); // true

!= operator

let a = 10, 
    b = 20;
    c = 10;	
console.log(a != c); // false
console.log(a != b); // true
console.log(a != 10); // false

!== operator

let a = 10, 
    b = 20;
    c = 10;	
console.log(a !== c); // false
console.log(a !== b); // true
console.log(a !== 10); // false

> operator

let a = 10, 
    b = 20;
    c = 10;	
console.log(b > c); // true
console.log(a > b); // false
console.log(a > 10); // false

 >= operator

let a = 10, 
    b = 20;
    c = 10;	
console.log(b >= c); // true
console.log(c >= b); // false
console.log(a >= 10); // true

 < operator

let a = 10, 
    b = 20;
    c = 10;	
console.log(b < c); // false
console.log(c < b); // true
console.log(a < b); // true

<= operator

let a = 10, 
    b = 20;
    c = 10;	
console.log(b <= c); // false
console.log(c <= b); // true
console.log(a <= b); // true

Comparing strings

If the operands are strings, JavaScript compares the character codes numerically one by one in the string.

let country1 = 'france',
    country2 = 'spain';    
let result = country1 < country2;

console.log(result); // true
console.log(country1 == 'france'); // true

Comparing Boolean

If an operand is a Boolean, JavaScript converts it to a number and compares the converted value with the other operand; true will be converted to a 1 and false will converted to a 0.

 

console.log(true > 0); // true
console.log(false < 1); // true
console.log(true > false); // true
console.log(false > true); // false

 

You may also like