DEV Community

loading...
Cover image for ✔||🤢 Commit or Vomit | + cast

✔||🤢 Commit or Vomit | + cast

jmdejager profile image Jasper de Jager ・1 min read

Photo by Mika Baumeister on Unsplash

Another ✔||🤢, casting to number with +;

Actually found this one in the angular documentation at some point.


// example: numberInputField.value = "3"
const startValue: number = +numberInputField.value;

Enter fullscreen mode Exit fullscreen mode

casting to number with + ✔||🤢

❤: Commit (something I could commit)
🏷: Vomit (I'd never commit this)
🦄: I like your post please continue this series!

Let's vote! 😊

Discussion (10)

pic
Editor guide
Collapse
technoglot profile image
Amelia Vieira Rosado

Oh boy, I'm not familiar with this one but I can say with great certainty that:

its a no from me

Collapse
technoglot profile image
Amelia Vieira Rosado

one more gif...

nope

Collapse
jmdejager profile image
Collapse
jmdejager profile image
Jasper de Jager Author

I'll start myself. Came across something like this in the Angular docs, didn't like it because it is too easy mis, so Vomit. 🤢

Collapse
angelomiranda profile image
angelo.miranda • Edited

it is a yuck for me, perhaps something more readable

Number('1')
parseInt('1', 10)
Enter fullscreen mode Exit fullscreen mode
Collapse
faraazahmad profile image
Collapse
jackmellis profile image
Jack

Yuck. This is something I would've done everywhere as a junior dev having just found about it 🤦 Not at all obvious and more likely someone would see this and assume it was a mistake and remove it

Collapse
johnkazer profile image
John Kazer

Make it explicit and also tell us what to do if the type doesn't match

Collapse
jankapunkt profile image
Jan Küster

ALWAYS use Number.parseInt and add the radix or you may end up in an undetermined state. developer.mozilla.org/en-US/docs/W...

Collapse
nefomemes profile image
Nefomemes

It's unorthodox, but it's a much better way to convert something into a number.