This implicit cast is equal to Number(numberInputField.value), not parseInt or parseFloat, like some in the comments suggest. Since all of javascripts typing is odd, this has some interesting edge cases, e.g:
+"0x4"//=> 4parseFloat("0x4")//=> 0+[]//=> 0parseFloat([])//=> NaN+null//=> 0parseFloat(null)//=> NaN+[12]//=> 12parseFloat([12])//=> 12+[12,12]//=> NaNparseFloat([12,12])//=> 12+{}//=> NaNparseFloat({})//=> NaN+{valueOf(){return12}}//=> 12parseFloat({valueOf(){return12}})//=> NaN+Symbol("12")//=> TypeError: Cannot convert a Symbol value to a numberparseFloat(Symbol("12"))//=> TypeError: Cannot convert a Symbol value to a string
However, you are using TypeScript, so I might actually let this slip! Once your codebase is explicitly properly typed, explicit casts can be considered redundant information.
In this specific case, I tend to say: Neither. Use a custom cast, that validates the value fits your specific number format, since parseFloat just silently ignores garbage, which could lead to an unintended result. Which cast you use in that function at the end is fairly irrelevant.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
This implicit cast is equal to
Number(numberInputField.value)
, notparseInt
orparseFloat
, like some in the comments suggest. Since all of javascripts typing is odd, this has some interesting edge cases, e.g:However, you are using TypeScript, so I might actually let this slip! Once your codebase is explicitly properly typed, explicit casts can be considered redundant information.
In this specific case, I tend to say: Neither. Use a custom cast, that validates the value fits your specific number format, since
parseFloat
just silently ignores garbage, which could lead to an unintended result. Which cast you use in that function at the end is fairly irrelevant.