Javascript String Comparison
String Comparison is an important thing to understand in Javascript. We need to make sure that we comprehend how string comparison works exactly with different operators like <
<=
>
>=
===
==
.
<
stands for less than. Soa < b
checks whethera
is less thanb
or not, and if that's true, then it evaluates to true, else it evaluates to false.<=
means less than OR equal to. Soa <= b
checkes whethera
is less than OR equal tob
. If any of those are true, then it evaluates to true. Else it evaluates to false.>
and>=
are similar to the above examples, but in the reverse order.===
checks whether 2 operands are strictly equal or not. So,a === b
checks whethera
andb
are strictly equal or not. By strictly, I mean, their types (strings, numbers, etc.) should be same and the values should also be same. It is recommended to use===
instead of==
because==
does some conversion (type coercion) when the 2 operands are of different types, causing weird bugs. So1234 == '1234'
is true while1234 === '1234'
evaluates to false.
This understanding will help us write our programs better, and make sure that we don't produce bugs by using the incorrect operator.
Note: String comparison is based on ASCII. Here's the ASCII Table: http://www.asciitable.com/
Related:
Warning: Invalid argument supplied for foreach() in /var/www/cssdeck.com/app/modules/labs/views/_details.php on line 68