DEV Community

Cover image for JavaScript basics comparison operators
Chris Bongers
Chris Bongers

Posted on • Originally published at daily-dev-tips.com

 

JavaScript basics comparison operators

In this article on JavaScript basics, we'll be looking at comparison operators.
These operators can be used to compare two values returning a boolean (true or false).

These are super handy for decision-making. Let's see which ones we can use:

Operator Comparison Example
== Equal to 8==8 // true
5==8 // false
'5'==5 // true
'f'=='f' // true
!= Not equal to 8!=8 // false
5!=8 // true
'5'!=5 // false
'f'!='f' // false
=== Strict equal to 8===8 // true
'5'===5 // false
'f'==='f' // true
!== Strict not equal to 8!==8 // false
'5'!==5 // true
'f'!=='f' // false
> Greater than 5>8 // false
8>5 // true
5>5 // false
< Less than 5<8 // true
8<5 // false
5<5 // false
>= Greater than or equal to 5>=8 // false
8>=5 // true
5>=5 // true
<= Less than or equal to 5<=8 // true
8<=5 // false
5<=5 // true

JavaScript equal to operator

This operator is used to evaluate two values. However, they don't have to be of the same type.
Meaning we can assess if a string is equal to a number!

`5` == 5; // true
5 == 5; // true
Enter fullscreen mode Exit fullscreen mode

But it can also compare strings for instance:

'string' == 'string'; // true
'String' == 'string'; // false
Enter fullscreen mode Exit fullscreen mode

Note: It's capital sensitive, as you can see above!

JavaScript not equal to operator

Following this is the not equal to operator, which can evaluate if a comparison is not correct.

5 != 5; // false
8 != 5; // true
'8' != 5; // true
'String' != 'string'; // true
'string' != 'string'; // false
Enter fullscreen mode Exit fullscreen mode

JavaScript strict operators

Then we have these two as strict versions, which should be preferred over the top ones.
What this means is that it will check against the type as well.

5 === 5; // true
'5' === 5; // false
Enter fullscreen mode Exit fullscreen mode

And the same works for the not equal to strict comparison.

5 !== 5; // false
8 !== 5; // true
'8' !== 5; // true
Enter fullscreen mode Exit fullscreen mode

Read more about == vs === in JavaScript.

JavaScript Greater and Less then

Then we have the greater than and less than operators.
These can be used to assess if a value is greater or less than the compared one.

Generally, these should only be used with number values.

8 > 5; // true
8 < 5; // false
5 > 8; // false
5 < 8; // true
5 > 5; // false
Enter fullscreen mode Exit fullscreen mode

JavaScript greater/less or equal to

We can also use the above two comparisons to check whether something hits a threshold.

We want to evaluate if a value is greater than or equal to a certain number?

5 >= 5; // true
8 >= 5; // true
Enter fullscreen mode Exit fullscreen mode

Meaning our number is greater than or equal to 5, which is the case in the above example.

This can also be used for checking less than operations.

5 <= 5; // true
3 <= 5; // true
Enter fullscreen mode Exit fullscreen mode

Thank you for reading, and let's connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter

Top comments (5)

Collapse
 
stuffsuggested profile image
Collapse
 
sadeegh profile image
Sadegh Rastgoo

Thank you for article โค๏ธ
Is JS compare string and number base on unicode? or am i wrong?is it possible in strict equal operator we recive true for compare a string and a number? (Example= 5 !== 'e')

Collapse
 
dailydevtips1 profile image
Chris Bongers

Hi Sadegh,

Glad you enjoyed the article.
String and number I don't think has anything to do with unicode yet,
in your example we would get true since it doesn't match.

Unicode only comes in when certain characters look a like, but have a different unicode entity. As far as i'm aware.

Collapse
 
imiahazel profile image
Imia Hazel

Thanks for the refresher. I mostly use Strictly Equal To operator.

Collapse
 
dailydevtips1 profile image
Chris Bongers

Glad you liked it Imia,
And indeed hardly a good usecase for the non-strict version

Visualizing Promises and Async/Await ๐Ÿคฏ

async await

โ˜๏ธ Check out this all-time classic DEV post