Example 1 of the knockout extenders page describes a way of rounding user input and making sure it is only numeric.
It works great, but looking through the source they do a peculiar thing that i don't understand, that is on line 8 they do this:
parseFloat(+newValue)
newValue
is a string.
When i initially asked this question I didn't know what +
did - some further poking and a link to a different MDN page from one of the initial answers I got indicate it is a unary operator equivalent to number(str)
and that there are some differences between +str
and parseFloat(str)
(treatment of strings ending in alpha characters and interpretation of hex seem to be the headlines).
I still don't understand why the +
in this case needed to be wrapped in the parseFloat
although I am starting to think it might be a typo...