>- greater than
<- less than
==- equal to; not to be confused with
===- strict equal to. This takes inputs literally with no conversions
>=- greater than or equal to
<=- less than or equal to
!=- not equal to
!==- strict not equal to
The Result of a Comparison
All comparison will return
true meaning correct there was a match and
false for no match.
2 = 1
2 != 1
You can assign the result of a comparison to a variable for use elsewhere in the program.
var result = 2 > 1;
a is less than
z, a number is always less than a letter, and a lowercase letter is always greater than an uppercase one.
console.log('a' > 'b');
console.log('a' > 'A');
console.log('a' > '6');
Since we know that strings are compared with each other one character at a time, it makes sense why the example below returns
console.log('abde' > 'abcd');
d is greater than
true. It doesn't matter what comes after d. For the above example to return
false each letter on the left side of the logic would have to be less than or equal to its counterpart on the right.
Compare Strings and Number Objects
console.log('2' > 1);
Strict Equal to
== also does not distinguish between
false, or an empty string and
console.log(0 == false);
console.log('' == false);
console.log(0 === false);
console.log('' === false);
Strict Not Equal to
You can perform a strict not equal to like this:
console.log(0 !== false);
Comparing null and undefined
There are two ways to compare
undefined depending on whether you need
false when comparing them.
console.log(null == undefined);
== (equals) converts
null and undefined to mean the same thing.
=== (strict equals) does not do this:
console.log(null === undefined);