7

What is the difference between Number.parseFloat() and parseFloat()? Is one better than the other?

5
  • 2
    developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Sep 28, 2018 at 18:02
  • 4
    "The Number.parseFloat() method parses a string argument and returns a floating point number. This method behaves identically to the global function parseFloat() and is part of ECMAScript 2015 (its purpose is modularization of globals)." Commented Sep 28, 2018 at 18:02
  • They are identical. ES6 just sweeped related globals under the same thing. Commented Sep 28, 2018 at 18:02
  • Better how? parsefloat() doesn't exist, so I guess that's worse? Commented Sep 28, 2018 at 18:02
  • The two behave exactly the same. Commented Sep 28, 2018 at 18:03

3 Answers 3

16

Neither

They are the exact same function. They don't only behave the same. They are the exact same function object.

To expand on that, Number.parseFloat() was created in ECMAScript 2015, as part of an effort to modularize globals [because global functions with no namespace makes me sad :(]

Sign up to request clarification or add additional context in comments.

4 Comments

@DavidWalschots I guess it's because it's related to the question. The question is bad, no doubt about it - it shows no research effort whatsoever, so it deserves downvotes. This answer is fine, though. Probably redundant, considering it's re-stating what's in the docs but that's OK, I think. It's the question that shouldn't be here if there was any real attempt to look at the docs.
Note that there is no guarantee at all that Number.parseFloat === parseFloat will remain true along time (but I doubt they will diverge, the most probable outcome is that parseFloat will disappear someday).
@VLAZ my 'research' brought me to this question and answer. It answered my question.. So as far as I am concerned, it's a great question, because it's the same question I had.
@VLAZ this had a score in google than the Docs.. so this came first, so I spotted it first, so no need to read the docs.
2

parseFloat vs. Number.parseFloat

parseFloat === Number.parseFloat // => true

--

parseFloat is accessible via the Global scope as of ES2015

parseFloat

From MDN: its purpose is modularization of globals

--

Number.parseFloat is available via a method on the Number object (also since ES2015).

In either case, the behavior for both function calls is identical. Both will type coerce the input into a Number if possible (e.g.: parseFloat('67') // => 67) or return NaN if the parsed input wasn't able to be coerced.

1 Comment

Both will type coerce the input into a Number if possible that is incorrect. As the name suggests, the function(s) will parse the input and extract a float. If you call parseFloat("12.34abc") the result would be 12.34 as the function will only stop parsing when it finds a character that cannot be used to build a number, thus extracting the numeric value from the beginning of the string. NaN will be the result if the input starts with a non-numeric character. parseFloat is accessible via the Global scope as of ES2015 also incorrect - it's in the spec as global from version 1.
2

As other people have said: they are exactly the same.

However(writing 2019), parseFloat has IE11 support, while Number.parseFloat does not.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/parseFloat

1 Comment

This is still the case - and also bootstrap 5.0 uses Number.parseFloat, which fails for IE 11.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.