logoalt Hacker News

joaohaastoday at 1:57 AM1 replyview on HN

What about the fact that there isn't a single 'parseInt' function in JS that can reliably only convert number strings to numbers?

They each have different quirks (some will parse 'a123' as 123, others will handle scientific notation etc). The only reliable way of doing this is doing a regex followed by parseInt... which is definitely a footgun IMO.


Replies

Izkatatoday at 3:15 AM

Unary "+" returns NaN for strings that don't contain exactly a number (except for empty string which standard type conversion turns into 0). It even works for scientific notation like +'1e3' === 1000.