+ 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

13th May 2022, 11:50 AM
🇺🇦 Drake CDR 🇺🇦
🇺🇦 Drake CDR 🇺🇦 - avatar
4 Respuestas
+ 15
Lisa, thanks 👍! Bob_Li, sure 👍!
13th May 2022, 11:59 AM
🇺🇦 Drake CDR 🇺🇦
🇺🇦 Drake CDR 🇺🇦 - avatar
+ 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.
14th May 2022, 12:30 PM
🇺🇦 Drake CDR 🇺🇦
🇺🇦 Drake CDR 🇺🇦 - avatar
+ 5
Why not parseFloat()? If we enter 12.0 for instance, JS will cut it down to an integer.
13th May 2022, 11:55 AM
Lisa
Lisa - avatar
+ 1
this will not throw you a NaN ~~'123.568' = 123 ~~'233abc' = 0 tricks are handy, but don't use them on serious calculations.
14th May 2022, 1:19 PM
Bob_Li
Bob_Li - avatar