I am trying to compare a value coming from a HTML text field with integers. And it works as expected. Condition is -
x >= 1 && x <= 999;
x is the value of text field. Condition returns
true whenever value is between 1-999 (inclusive), else
Problem is, that the value coming from the text field is of string type and I'm comparing it with integer types. Is it okay to have this comparison like this or should I use parseInt() to convert
x to integer ?
<= (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
>=, 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. So
x is ignored. Supports an optional radix (number base) argument, so
parseInt("15", 16) is
15 in hex). If there's no radix, assumes decimal unless the string starts with
0X), in which case it skips those and assumes hex. Does not look for the new
0b (binary) or
0o (new style octal) prefixes; both of those parse as
0. (Some browsers used to treat strings starting with
0 as octal; that behavior was never specified, and was [specifically disallowed] in the ES5 specification.) Returns
NaN if no parseable digits are found.
Number.parseInt(str[, radix]) - Exactly the same function as
parseInt above. (Literally,
Number.parseInt === parseInt is
parseFloat(str) - Like
parseInt, but does floating-point numbers and only supports decimal. Again extra characters on the string are ignored, so
x is ignored). As only decimal is supported,
0 (because parsing ends at the
NaN if no parseable digits are found.
Number.parseFloat(str) - Exactly the same function as
0x prefix = hex;
0b = binary [ES2015+];
0o prefix = octal [ES2015+]; some implementations extend it to treat a leading
0 as octal, but not in strict mode).
NaN because the
x is not ignored.
5 [ES2015+]. Has a gotcha:
NaN 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 converts
0 if the string cannot be converted to a valid number).
So if it's okay that extra bits on the string are ignored,
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
You should use
parseInt if the var is a string. Add = to compare
parseInt(x) >== 1 && parseInt(x) <== 999;
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.
©2020 All rights reserved.