Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

4
  • 358
    small hint: parseInt(null) returns NaN but +null returns 0 Commented Feb 1, 2018 at 14:46
  • 2
    While I wouldn't say that TypeScript is just JavaScript, in this case the answer is indeed "do that exactly the same way as in JavaScript". All techniques from all the answers on this page behave correctly type-wise. I cannot see how any of them is more or less "TypeScript way". The solution should be chosen after evaluating how each technique behaves on corner cases. See: medium.com/@nikjohn/… Commented Jun 1, 2020 at 21:40
  • 1
    +undefined will also return NaN Commented Mar 3, 2021 at 9:36
  • In this case, one difference between TypeScript and JavaScript: parseInt(variable, 10) triggers an error in Typescript if the variable if of type string | number and works just fine in JavaScript. Commented Aug 30, 2022 at 12:49