JavaScript basics comparison operators

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
This concept has always bugged me for so long time. Thanks for explaining it in a simple way.
This is super cool to hear Kartik!
Glad you enjoyed it ❤️
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

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 // true5==8 // false'5'==5 // true'f'=='f' // true |
!= | Not equal to | 8!=8 // false5!=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 // false8>5 // true5>5 // false |
< | Less than | 5<8 // true8<5 // false5<5 // false |
>= | Greater than or equal to | 5>=8 // false8>=5 // true5>=5 // true |
<= | Less than or equal to | 5<=8 // true8<=5 // false5<=5 // true |
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
But it can also compare strings for instance:
'string' == 'string'; // true
'String' == 'string'; // false
Note: It's capital sensitive, as you can see above!
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
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
And the same works for the not equal to strict comparison.
5 !== 5; // false
8 !== 5; // true
'8' !== 5; // true
Read more about == vs === in JavaScript.
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
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
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
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter