Subscribe

Vanilla JavaScript Comparison Operators

โœ๏ธ

Lets dive into all the possible operators in JavaScript

9 Jun, 2020 ยท 2 min read

Yesterday we looked at the difference between the == and === comparison operator. These are used to compare two values or objects. But there are more comparison operators in JavaScript we can leverage.

JavaScript Comparison Operators

Within JavaScript, we can leverage the following comparison operators. I've written down a table below to have a quick reference.

OperatorDescriptionComparingReturn
==Equal tox == 10
x == 3
x == "3"
false
true
true
===Equal value and typex === 3
x === "3"
true
false
!=Not equalx != 10
x != "3"
true
false
!==Not equal value and typex !== "3"
x !== 3
true
false
>Bigger thanx > 2true
<Smaller thanx < 4true
>=Bigger than or equalx >= 3true
<=Smaller than or equalx <= 3true

The == and === we discussed in detail yesterday.

JavaScript != and !== Comparison

As you can guess, these are very similar to the == and === but the ! Means not in most programming languages. So these will compare if the value is NOT what we compare it to. And the !== will even check for the correct type.

x = 3;

<!-- != comparison -->
console.log(x != 10) // true
console.log(x != "3") // false

<!-- !== comparison -->
console.log(x !== "3") // true
console.log(x !== 3) // false

As you can see in the second example it will think the string number three is wrong.

JavaScript Smaller and Bigger Than Comparison

The next ones are all in regards to smaller than or bigger than:

<!-- > comparison -->
console.log(x > 2) // true

<!-- < comparison -->
console.log(x < 4) // true

<!-- >= comparison -->
console.log(x >= 3) // true

<!-- <= comparison -->
console.log(x <= 3) // true

So note > is just for bigger than and >= also includes the actual number itself. Same goes for < and <=.

Feel free to explore the following Codepen.

See the Pen Vanilla JavaScript Comparison Operators by Chris Bongers (@rebelchris) on CodePen.

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

Spread the knowledge with fellow developers on Twitter
Tweet this tip
Powered by Webmentions - Learn more

Read next ๐Ÿ“–

JavaScript sending data between windows

9 Sep, 2022 ยท 4 min read

JavaScript sending data between windows

Using the native payment request JavaScript API

9 Aug, 2022 ยท 8 min read

Using the native payment request JavaScript API

Join 1829 devs and subscribe to my newsletter