What is the difference between Number(…) and parseFloat(…)

前端 未结 5 1426
Happy的楠姐
Happy的楠姐 2020-12-05 18:04

What is the difference between parseInt(string) and Number(string) in JavaScript has been asked previously.

But the answers basically focused on the radix

5条回答
  •  隐瞒了意图╮
    2020-12-05 18:33

    When not using new to create a wrapper object for a numerical value, Number is relegated to simply doing type conversion from string to number.

    'parseFloat' on the other hand, as you mentioned, can parse a floating point number from any string that starts with a digit, a decimal, or +/-

    So, if you're only working with strings that contain only numerical values, Number(x) and parseFloat(x) will result in the same values

提交回复
热议问题