Comparison Operators
JavaScript is what's considered to be a loosely-typed language. In other words, the programmer does not need to specify the type (integer, string, etc.) of a variable or object, or the return type of a function for that matter. With that said, there are two types of comparison operators in JavaScript - strict and type-converting.
Equality Operators
With a type-converting comparison, we use ==
, or the "double-equals" to compare two items. The interpreter will convert both operands in question into the same type. From there it will then perform a strict comparison. As we can see below, number
has a value of 5
, and numberAsAString
has a value of "5"
. Once the interpreter converts them to the same type, either string or integer, it returns true
, as both values are equal.
// type-converting comparison
const number = 5;
const numberAsAString = "5";
number == numberAsAString // returns true
With a strict comparison, we use ===
, or the "triple-equals" to compare two items. The interpreter does not give you the luxury of converting both operands to the same type. It strictly checks if they're the same type - and if so, it will then check if their values are equal. Otherwise, it returns false
. Here's an example:
// strict comparison
const number = 5;
const numberAsAString = "5";
number == numberAsAString // returns false;
Comparing Objects
It's important to note that when comparing objects, the interpreter does not just compare the values - it compares the objects reference in memory.
let thisObj = { id: 5 };
let thatObj = { id: 5 };
thisObj == thatObj // returns false
thisObj === thatObj // returns false
let theOtherObj = thisObj;
thisObj == theOtherObj; // returns true
thisObj === theOtherObj; // returns true
Inequality Operators
On the other hand, we have inequality (!=
) and strict inequality (!==
) operators.
In a similar fashion, the inequality operator will check if two items are not equal, whilst ignoring type.
let myNumber = 100;
let myString = "50";
myNumber != myString // returns true
The strict inequality operator will check the types of both operands. If they're different types, then it will return true as they're not equal. If they are of the same type, but their respective values are different, it will return true.
let itemA = 3;
let itemB = "3";
itemA !== itemB // returns true
Relational Operators
Lastly, we have the relational operators which may look familiar:
- < is the 'less than' operator
- > is the 'greater than' operator
- <= is the 'less than or equal to' operator
- >= is the 'greater than or equal to' operator
let foo = 5;
let bar = 10;
let baz = 10;
foo < bar // returns true
foo > bar // returns false
bar >= baz // returns true
bar <= baz // returns true
Wrapping Up ✅
Comparison operators are certainly one of the fundamentals of programming as they allow us to compare values in our logic to one another and execute code based off of the result that's the returned.
Stay Tuned 📺
If you have any questions or improvements, or if you'd like to see additional examples, feel free to reach out to me anytime at tim@timwheeler.com
. If you're enjoying my posts, please click here 📬 or subscribe below 👇!