Why does string to number comparison work in Javascript
Solution 1:
Because JavaScript defines >=
and <=
(and several other operators) in a way that allows them to coerce their operands to different types. It's just part of the definition of the operator.
In the case of <
, >
, <=
, and >=
, the gory details are laid out in §11.8.5 of the specification. The short version is: If both operands are strings (after having been coerced from objects, if necessary), it does a string comparison. Otherwise, it coerces the operands to numbers and does a numeric comparison.
Consequently, you get fun results, like that "90" > "100"
(both are strings, it's a string comparison) but "90" < 100
(one of them is a number, it's a numeric comparison). :-)
Is it okay to have this comparison like this or should I use parseInt() to convert x to integer ?
That's a matter of opinion. Some people think it's totally fine to rely on the implicit coercion; others think it isn't. There are some objective arguments. For instance, suppose you relied on implicit conversion and it was fine because you had those numeric constants, but later you were comparing x
to another value you got from an input field. Now you're comparing strings, but the code looks the same. But again, it's a matter of opinion and you should make your own choice.
If you do decide to explicitly convert to numbers first, parseInt
may or may not be what you want, and it doesn't do the same thing as the implicit conversion. Here's a rundown of options:
parseInt(str[, radix])
- Converts as much of the beginning of the string as it can into a whole (integer) number, ignoring extra characters at the end. SoparseInt("10x")
is10
; thex
is ignored. Supports an optional radix (number base) argument, soparseInt("15", 16)
is21
(15
in hex). If there's no radix, assumes decimal unless the string starts with0x
(or0X
), in which case it skips those and assumes hex. Does not look for the new0b
(binary) or0o
(new style octal) prefixes; both of those parse as0
. (Some browsers used to treat strings starting with0
as octal; that behavior was never specified, and was [specifically disallowed][2] in the ES5 specification.) ReturnsNaN
if no parseable digits are found.Number.parseInt(str[, radix])
- Exactly the same function asparseInt
above. (Literally,Number.parseInt === parseInt
istrue
.)parseFloat(str)
- LikeparseInt
, but does floating-point numbers and only supports decimal. Again extra characters on the string are ignored, soparseFloat("10.5x")
is10.5
(thex
is ignored). As only decimal is supported,parseFloat("0x15")
is0
(because parsing ends at thex
). ReturnsNaN
if no parseable digits are found.Number.parseFloat(str)
- Exactly the same function asparseFloat
above.Unary
+
, e.g.+str
- (E.g., implicit conversion) Converts the entire string to a number using floating point and JavaScript's standard number notation (just digits and a decimal point = decimal;0x
prefix = hex;0b
= binary [ES2015+];0o
prefix = octal [ES2015+]; some implementations extend it to treat a leading0
as octal, but not in strict mode).+"10x"
isNaN
because thex
is not ignored.+"10"
is10
,+"10.5"
is10.5
,+"0x15"
is21
,+"0o10"
is8
[ES2015+],+"0b101"
is5
[ES2015+]. Has a gotcha:+""
is0
, notNaN
as you might expect.Number(str)
- Exactly like implicit conversion (e.g., like the unary+
above), but slower on some implementations. (Not that it's likely to matter.)Bitwise OR with zero, e.g.
str|0
- Implicit conversion, like+str
, but then it also converts the number to a 32-bit integer (and convertsNaN
to0
if the string cannot be converted to a valid number).
So if it's okay that extra bits on the string are ignored, parseInt
or parseFloat
are fine. parseInt
is quite handy for specifying radix. Unary +
is useful for ensuring the entire string is considered. Takes your choice. :-)
And finally: If you're converting to number and want to know whether the result is NaN
, you might be tempted to do if (convertedValue === NaN)
. But that won't work, because as Rick points out below, comparisons involving NaN
are always false. Instead, it's if (isNaN(convertedValue))
.
Solution 2:
The MDN's docs on Comparision states that the operands are converted to a common type before comparing (with the operator you're using):
The more commonly used abstract comparison (e.g. ==) converts the operands to the same Type before making the comparison. For relational abstract comparisons (e.g., <=), the operands are first converted to primitives, then to the same type, before comparison.
You'd only need to apply parseInt()
if you were using a strict comparision, that does not perform the auto casting before comparing.