The parseFloat()
function parses a string argument and returns a floating point number.
The parseFloat()
function parses a string argument and returns a floating point number.
function circumference(r) {
return parseFloat(r) * 2.0 * Math.PI;
}
console.log(circumference(4.567));
/ Expected output: 28.695307297889173
console.log(circumference("4.567abcdefgh"));
/ Expected output: 28.695307297889173
console.log(circumference("abcdefgh"));
/ Expected output: NaN
parseFloat(string)
string
The value to parse, whitespace in this argument is ignored.
The parseFloat
function converts its first argument to a string, parses that string as a decimal number literal, then returns a number or NaN
. The number syntax it accepts can be summarized as:
parseFloat()
are plus sign (+
), minus sign (-
U+002D HYPHEN-MINUS), decimal digits (0
– 9
), decimal point (.
), exponent indicator (e
or E
), and the "Infinity"
literal.+
/-
signs can only appear strictly at the beginning of the string, or immediately following the e
/E
character. The decimal point can only appear once, and only before the e
/E
character. The e
/E
character can only appear once, and only if there is at least one digit before it.parseFloat()
can also parse and return Infinity
or -Infinity
if the string starts with "Infinity"
or "-Infinity"
preceded by none or more white spaces.parseFloat()
picks the longest substring starting from the beginning that generates a valid number literal. If it encounters an invalid character, it returns the number represented up to that point, ignoring the invalid character and all characters following it.parseFloat
returns NaN
.Syntax-wise, parseFloat()
parses a subset of the syntax that the Number()
function accepts. Namely, parseFloat()
does not support non-decimal literals with 0x
, 0b
, or 0o
prefixes but supports everything else. However, parseFloat()
is more lenient than Number()
because it ignores trailing invalid characters, which would cause Number()
to return NaN
.
Similar to number literals and Number()
, the number returned from parseFloat()
may not be exactly equal to the number represented by the string, due to floating point range and inaccuracy. For numbers outside the -1.7976931348623158e+308
– 1.7976931348623158e+308
range (see Number.MAX_VALUE
), -Infinity
or Infinity
is returned.
The following examples all return 3.14
:
parseFloat(3.14);
parseFloat("3.14");
parseFloat(" 3.14 ");
parseFloat("314e-2");
parseFloat("0.0314E+2");
parseFloat("3.14some non-digit characters");
parseFloat({
toString() {
return "3.14";
},
});
The following example returns NaN
:
parseFloat("FF2");
Anecdotally, because the string NaN
itself is invalid syntax as accepted by parseFloat()
, passing "NaN"
returns NaN
as well.
parseFloat("NaN"); / NaN
Infinity values are returned when the number is outside the double-precision 64-bit IEEE 754-2019 format range:
parseFloat("1.7976931348623159e+308"); / Infinity
parseFloat("-1.7976931348623159e+308"); / -Infinity
Infinity is also returned when the string starts with "Infinity"
or "-Infinity"
:
parseFloat("Infinity"); / Infinity
parseFloat("-Infinity"); / -Infinity
parseFloat()
does not handle BigInt
values. It stops at the n
character, and treats the preceding string as a normal integer, with possible loss of precision. If a BigInt value is passed to parseFloat()
, it will be converted to a string, and the string will be parsed as a floating-point number, which may result in loss of precision as well.
parseFloat(900719925474099267n); / 900719925474099300
parseFloat("900719925474099267n"); / 900719925474099300
You should pass the string to the BigInt()
function instead, without the trailing n
character.
BigInt("900719925474099267");
/ 900719925474099267n
Specification |
---|
ECMAScript® 2026 Language Specification # sec-parsefloat-string |