11

From documentation:

This method behaves identically to the global function parseInt()

But, as it's experimental, the compatibility is worst. For example, is not available in IE or Safari.

So, why should a developer use Number.parseInt()?

6
  • because the developer uses a JS Engine that supports it? Commented Feb 5, 2015 at 17:25
  • 6
    I don't think there's a better reason to use it. The function is exactly the same. They're just trying to remove global functions in ECMAScript 6, and making them class methods. Commented Feb 5, 2015 at 17:26
  • possible duplicate of What is the difference between parseInt(string) and Number(string) in JavaScript?
    – Mike Cheel
    Commented Feb 5, 2015 at 17:26
  • 9
    From the docs you've linked: "its purpose is modularization of globals".
    – Teemu
    Commented Feb 5, 2015 at 17:27
  • 2
    The problem it's not supported in IE
    – DanKodi
    Commented Aug 3, 2017 at 5:44

1 Answer 1

14

The use of Number.parseInt being encouraged over parseInt() is due to a trend in the JavaScript community of get away from using globals. The Mozilla documentation on Number.parseInt states:

...and is part of ECMAScript 2015 (its purpose is modularization of globals).

There you have it. It's because of global-phobia :)

2
  • What trend? I have never stumble up on someone ever using Number.NaN over just writing NaN. Maybe it's just me living under a rock
    – Endless
    Commented Jun 18, 2021 at 9:04
  • Yes it is you living under a rock. Commented Sep 28, 2023 at 6:36

Not the answer you're looking for? Browse other questions tagged or ask your own question.