+ 17
Do you know more non standard ways to convert user input into any number, not specifically an integer or a float 🙏?
Recently I found out, that adding a "+" before a prompt() or getting some value from the DOM element changes string data to number type. Not having a numerical value results in NaN. Good enough for me. It works fine, when you don't know what the user input will be. You can later check if that number was an integer or a float, which would be impossible with predefine data types. Weak typing is a JavaScript feature and I'd like to play around with it. https://code.sololearn.com/W3RHtb6bv1dp/?ref=app
4 Réponses
+ 6
Hey, turns out dividing or multiplying the value by 1 gives the same result as using plus.
Minus can also be used, when you want to get a negative number.
+ 5
Why not parseFloat()? If we enter 12.0 for instance, JS will cut it down to an integer.
+ 1
this will not throw you a NaN
~~'123.568' = 123
~~'233abc' = 0
tricks are handy, but don't use them on serious calculations.