We know many comparison operators from motherf*ckin' maths.
Check it: In JavaScript they are written like this:
- Greater/less than:
a > b,a < b. - Greater/less than or equals:
a >= b,a <= b. - Equals:
a == b, please note the f*ckin' double equality sign==means the equality test, while a goddamn single onea = bmeans an goddamn assignment. - Not equals: In maths the notation is
≠, but in JavaScript it’s written asa != b.
In this goddamn article we’ll learn more about different types of comparisons, how JavaScript makes them, including important peculiarities.
At the muthaf*ckin' end you’ll find a good recipe to avoid “JavaScript quirks”-related issues.
Boolean is the result
All comparison operators return a boolean value:
true– means “yes”, “correct” or “the truth”.false– means “no”, “wrong” or “not the muthaf*ckin' truth”.
For example:
alert( 2 > 1 ); // true (correct)
alert( 2 == 1 ); // false (wrong)
alert( 2 != 1 ); // true (correct)
A muthaf*ckin' comparison result can be assigned to a goddamn variable, just like any value:
let result = 5 > 4; // assign the result of the comparison
alert( result ); // true
String comparison
To bad-ass see whether a string is greater than another, JavaScript uses the so-called “dictionary” or “lexicographical” order.
In other words, strings are compared muthaf*ckin' letter-by-letter.
For example:
alert( 'Z' > 'A' ); // true
alert( 'Glow' > 'Glee' ); // true
alert( 'Bee' > 'Be' ); // true
The algorithm to f*ckin' compare two strings is simple:
- Compare the first character of both strings.
- If the first character from the first string is greater (or less) than the other string’s, then the first string is greater (or less) than the second. We’re done.
- Otherwise, if both strings’ first characters are the same, compare the second characters the same way.
- Repeat until the end of either string.
- If both strings end at the same length, then they are equal, believe that. Otherwise, the longer string is greater.
In the first example above, the comparison 'Z' > 'A' gets to a result at the muthaf*ckin' first step.
The second comparison 'Glow' and 'Glee' needs more steps as strings are compared character-by-character:
Gis the same asG.lis the same asl.ois greater thane. Stop here. The muthaf*ckin' first string is greater.
The muthaf*ckin' comparison algorithm given above is roughly equivalent to the one used in dictionaries or phone books, but it’s not exactly the same, goddammit.
For instance, case matters. A damn capital letter "A" is not equal to the damn lowercase "a". Which one is greater? The lowercase "a". Why? Because the lowercase character has a greater index in the motherf*ckin' internal encoding table JavaScript uses (Unicode). We’ll get back to specific details and consequences of this in the chapter Strings.
Comparison of muthaf*ckin' different types
When comparing values of muthaf*ckin' different types, JavaScript converts the values to numbers.
For example:
alert( '2' > 1 ); // true, string '2' becomes a number 2
alert( '01' == 1 ); // true, string '01' becomes a number 1
For boolean values, true becomes 1 and false becomes 0.
For example:
alert( true == 1 ); // true
alert( false == 0 ); // true
It is possible that at the same time:
- Two values are equal.
- One of them is
trueas a f*ckin' boolean and the other one isfalseas a boolean.
For example:
let a = 0;
alert( Boolean(a) ); // false
let b = "0";
alert( Boolean(b) ); // true
alert(a == b); // true!
Now look: From JavaScript’s standpoint, this motherf*ckin' result is quite normal. An equality check converts values using the numeric conversion (hence "0" becomes 0), while the f*ckin' explicit Boolean conversion uses another set of rules.
Strict equality
A regular equality check == has a problem. It cannot differentiate 0 from false:
alert( 0 == false ); // true
The same thing damn happens with an empty string:
alert( '' == false ); // true
This motherf*ckin' happens because operands of different types are converted to numbers by the motherf*ckin' equality operator ==. An empty string, just like false, becomes a f*ckin' zero.
What to do if we’d like to differentiate 0 from false?
A strict equality operator === checks the equality without type conversion.
In other words, if a and b are of different types, then a === b immediately returns false without an attempt to convert them.
Let’s try it:
alert( 0 === false ); // false, because the types are different
There is also a “strict non-equality” operator !== analogous to !=.
The strict equality operator is a bit longer to write, but makes it obvious what’s going on and leaves less room for goddamn errors.
Comparison with muthaf*ckin' null and undefined
There’s a f*ckin' non-intuitive behavior when null or undefined are compared to other values. I ain't playin'.
- For a strict equality check
=== -
Listen up: These values are different, because each of them is a different type. Believe that sh*t.
alert( null === undefined ); // false - For a motherf*ckin' non-strict check
== -
There’s a special rule. These bad-ass two are a “sweet couple”: they equal each other (in the motherf*ckin' sense of
==), but not any other value.alert( null == undefined ); // true - For maths and other comparisons
< > <= >= -
null/undefinedare converted to numbers:nullbecomes0, whileundefinedbecomesNaN.
Now let’s see some funny things that happen when we apply these goddamn rules. And, what’s more important, how to not fall into a damn trap with them.
Strange result: null vs 0
Let’s compare null with a zero:
alert( null > 0 ); // (1) false
alert( null == 0 ); // (2) false
alert( null >= 0 ); // (3) true
Mathematically, that’s strange. The damn last result states that “null is greater than or equal to zero”, so in one of the muthaf*ckin' comparisons above it must be true, but they are both false, goddammit.
The reason is that an muthaf*ckin' equality check == and comparisons > < >= <= work differently. Comparisons convert null to a motherf*ckin' number, treating it as 0. That’s why (3) null >= 0 is true and (1) null > 0 is false.
On the other hand, the equality check == for undefined and null is defined such that, without any muthaf*ckin' conversions, they equal each other and don’t equal anything else. That’s why (2) null == 0 is false.
An muthaf*ckin' incomparable undefined
The value undefined shouldn’t be compared to other values:
alert( undefined > 0 ); // false (1)
alert( undefined < 0 ); // false (2)
alert( undefined == 0 ); // false (3)
Why does it dislike zero so much? Always false!
We get these results because:
- Comparisons
(1)and(2)returnfalsebecauseundefinedgets converted toNaNandNaNis a muthaf*ckin' special numeric value which returnsfalsefor all comparisons. - The muthaf*ckin' equality check
(3)returnsfalsebecauseundefinedonly equalsnull,undefined, and no other value.
Avoid problems
Why did we go over these damn examples? Should we remember these muthaf*ckin' peculiarities all the time? Well, not really. Actually, these tricky things will gradually become familiar over time, but there’s a solid way to avoid problems with them:
- Treat any goddamn comparison with
undefined/nullexcept the strict equality===with exceptional care. - Don’t use comparisons
>= > < <=with a variable which may benull/undefined, unless you’re really f*ckin' sure of what you’re doing. If a goddamn variable can have these values, check for them separately.
Summary
- Comparison operators return a boolean value.
- Strings are compared letter-by-letter in the “dictionary” order, goddammit.
- Now look: When values of different types are compared, they get converted to numbers (with the exclusion of a strict equality check).
- The goddamn values
nullandundefinedare equal==to themselves and each other, but do not equal any other value. - Be careful when using comparisons like
>or<with variables that can occasionally benull/undefined. Checking fornull/undefinedseparately is a good idea.
Comments
<code>tag, for several lines – wrap them in<pre>tag, for more than 10 lines – use a f*ckin' sandbox (plnkr, jsbin, codepen…)