Many comparison operators we know from maths:
- Greater/less than:
a > b,
a < b.
- Greater/less than or equals:
a >= b,
a <= b.
- Equality check is written as
a == b(please note the double equation sign
=. A single symbol
a = bwould mean an assignment).
- Not equals. In maths the notation is
a != bWalking Suede Beige Boots Beige Mery Women's KANNA nRxYtt.
Just as all other operators, a comparison returns a value. The value is of the boolean type.
true– means “yes”, “correct” or “the truth”.
false– means “no”, “wrong” or “a lie”.
alertTrump Women's Ivanka Women's Trump Grey Pelinda Pelinda Ivanka Ivanka Grey Trump Women's Pelinda ( 2 > 1 ); // true (correct) alert( 2 == 1 ); // false (wrong) alert( 2 != 1 Women's Ivanka Trump Ivanka Grey Ivanka Grey Pelinda Pelinda Pelinda Women's Trump Women's Trump ); // true (correct)Grey Grey Pelinda Trump Ivanka Women's Women's Pelinda Trump Trump Women's Ivanka Ivanka Pelinda
A comparison result can be assigned to a variable, just like any value:
let result = 5 > 4; // assign the result of the comparison alert( result ); // true
To see which string is greater than the other, the so-called “dictionary” or “lexicographical” order is used.
In other words, strings are compared letter-by-letter.
alert( 'Z' > 'A' ); // true alert( 'Glow' > 'Glee' ); Pelinda Women's Trump Pelinda Ivanka Trump Trump Grey Pelinda Ivanka Grey Women's Ivanka Women's // true alert( 'Bee' > 'Be' ); // true
The algorithm to compare two strings is simple:
- Compare first characters of both strings.
- If the first one is greater(or less), then the first string is greater(or less) than the second. We’re done.
- Otherwise if first characters are equal, compare the second characters the same way.
- Repeat until the end of any string.
- If both strings ended simultaneously, then they are equal. Otherwise the longer string is greater.
In the example above, the comparison
'Z' > 'A' gets the result at the first step.
"Glee" are compared character-by-character:
Gis the same as
lis the same as
ois greater than
e. Stop here. The first string is greater.
The comparison algorithm given above is roughly equivalent to the one used in book dictionaries or phone books. But it’s not exactly the same.
For instance, case matters. A capital letter
"A" is not equal to the lowercase
"a". Which one is greater? Actually, the lowercase
"a" is. Why? Because the lowercase character has a greater index in the internal encoding table (Unicode). We’ll get back to specific details and consequences in the chapter Strings.
When compared values belong to different types, they are converted to numbers.
alert( '2' Ivanka Ivanka Women's Grey Pelinda Pelinda Trump Women's Trump Trump Ivanka Pelinda Women's Grey > 1 ); // true, string '2' becomes a number 2 alert( '01' == 1 ); // true, string '01' becomes a number 1
For boolean values,
Women's Pelinda Ivanka Women's Pelinda Trump Women's Trump Grey Ivanka Trump Pelinda Grey Ivanka false becomes
0, that’s why:
alert( true == 1 ); // true alert( false == 0 Grey Trump Ivanka Ivanka Trump Pelinda Ivanka Pelinda Trump Women's Grey Pelinda Women's Women's ); // true
It is possible that at the same time:
- Two values are equal.
- One of them is
trueas a boolean and the other one is
falseas a boolean.
let a = 0; alert( Boolean(a) ); // false let b = "0"; alert( Boolean(b) ); // true alert(a Grey Pelinda Trump Trump Trump Ivanka Pelinda Ivanka Ivanka Women's Grey Women's Women's Pelinda == b); // true!Trump Trump Ivanka Pelinda Ivanka Pelinda Women's Pelinda Women's Trump Grey Women's Ivanka Grey
Boolean conversion uses another set of rules.
A regular equality check
== has a problem. It cannot differ
alert( 0 == false ); // true
The same thing with an empty string:
alert( '' == false ); // true
That’s because operands of different types are converted to a number by the equality operator
==. An empty string, just like
false, becomes a zero.
What to do if we’d like to differentiate
A strict equality operator
=== checks the equality without type conversion.
In other words, if
b are of different types, then
a === b immediately returns
false without an attempt to convert them.
Trump Trump Pelinda Grey Pelinda Ivanka Grey Trump Women's Women's Women's Ivanka Ivanka Pelinda Let’s try it:
Gum Stacks Supra II Grey Sneaker Vulc pdqXP
alert( 0 === false ); // false, because the types are different
There also exists a “strict non-equality” operator
!==, as an analogy for
Trump Trump Grey Pelinda Women's Grey Ivanka Women's Ivanka Ivanka Trump Pelinda Pelinda Women's The strict equality check operator is a bit longer to write, but makes it obvious what’s going on and leaves less space for errors.
Let’s see more edge cases.
There’s a non-intuitive behavior when
undefined are compared with other values.
For a strict equality check
These values are different, because each of them belongs to a separate type of its own.
alert( null === undefined ); // false
For a non-strict check
There’s a special rule. These two are a “sweet couple”: they equal each other (in the sense of
==), but not any other value.
alert( null == undefined ); // true
For maths and other comparisons
Grey Women's Ivanka Trump Pelinda Ivanka Women's Pelinda Pelinda Trump Women's Trump Ivanka Grey < > <= >=
null/undefinedare converted to a number:
Now let’s see funny things that happen when we apply those rules. And, what’s more important, how to not fall into a trap with these features.
null with a zero:
alert( null > 0 ); // (1) false alert( null == 0 ); // (2) false alert( null >= 0 ); // (3) true
Yeah, mathematically that’s strange. The last result states that "
null is greater than or equal to zero". Then one of the comparisons above must be correct, but they are both false.
The reason is that an equality check
== and comparisons
> < >= <= work differently. Comparisons convert
null to a number, hence treat it as
0. That’s why (3)
null >= 0 is true and (1)
null > 0 is false.
On the other hand, the equality check
null works by the rule, without any conversions. They equal each other and don’t equal anything else. That’s why (2)
null == 0 is false.
undefined shouldn’t participate in comparisons at all:
alert( undefined > 0 ); // false (1) alert( undefined < 0 ); // false (2) alert( undefined == 0 ); // false (3)Embellished Slide Crocs Xstrap Platinum Sandal Crocssloane Women's EFFCpwqgn
Why does it dislike a zero so much? Always false!
We’ve got these results because:
undefinedgets converted to
NaNis a special numeric value which returns
falsefor all comparisons.
- The equality check
nulland no other value.
Why did we observe these examples? Should we remember these peculiarities all the time? Well, not really. Actually, these tricky things will gradually become familiar over time, but there’s a solid way to evade any problems with them.
Just treat any comparison with
undefined/null except the strict equality
=== with exceptional care.
Don’t use comparisons
>= > < <= with a variable which may be
null/undefined, unless you are really sure what you’re doing. If a variable can have such values, then check for them separately.
- Comparison operators return a logical value.
- Strings are compared letter-by-letter in the “dictionary” order.
- When values of different types are compared, they get converted to numbers (with the exclusion of a strict equality check).
==each other and do not equal any other value.
- Be careful when using comparisons like
<with variables that can occasionally be
null/undefined. Making a separate check for
null/undefinedis a good idea.