Converting from one type to another frequently causes bugs or unexpected result if you’re not careful. For example, when converting a string to an integer, we use the following function:
Accidentally, I used an array of numeric strings in place of string for conversion.
As stated in MDN parseInt:
If string is not a string, then it is converted to one. Leading whitespace in the string is ignored.
The array is converted to string.
If the array has only one element, then there won’t be any problem:
But when there are more than one element, only the first element will be parsed:
Therefore, you have to be very careful when processing query parameter like:
In many cases, it will be converted to:
After running through
parseInt, it will result in
2013, might not be what you are looking for.