parseFloat()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
TheparseFloat() function parses a string argument and returns a floating point number.
In this article
Try it
function circumference(r) { return parseFloat(r) * 2.0 * Math.PI;}console.log(circumference(4.567));// Expected output: 28.695307297889173console.log(circumference("4.567abcdefgh"));// Expected output: 28.695307297889173console.log(circumference("abcdefgh"));// Expected output: NaNSyntax
parseFloat(string)Parameters
stringThe value to parse,coerced to a string. Leadingwhitespace in this argument is ignored.
Return value
A floating point number parsed from the givenstring, orNaN when the first non-whitespace character cannot be converted to a number.
Note:JavaScript does not have the distinction of "floating point numbers" and "integers" on the language level.parseInt() andparseFloat() only differ in their parsing behavior, but not necessarily their return values. For example,parseInt("42") andparseFloat("42") would return the same value: aNumber 42.
Description
TheparseFloat function converts its first argument to a string, parses that string as a decimal number literal, then returns a number orNaN. The number syntax it accepts can be summarized as:
- The characters accepted by
parseFloat()are plus sign (+), minus sign (-U+002D HYPHEN-MINUS), decimal digits (0–9), decimal point (.), exponent indicator (eorE), and the"Infinity"literal. - The
+/-signs can only appear strictly at the beginning of the string, or immediately following thee/Echaracter. The decimal point can only appear once, and only before thee/Echaracter. Thee/Echaracter can only appear once, and only if there is at least one digit before it. - Leading spaces in the argument are trimmed and ignored.
parseFloat()can also parse and returnInfinityor-Infinityif 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.- If the argument's first character can't start a legal number literal per the syntax above,
parseFloatreturnsNaN.
Syntax-wise,parseFloat() parses a subset of the syntax that theNumber() function accepts. Namely,parseFloat() does not support non-decimal literals with0x,0b, or0o prefixes but supports everything else. However,parseFloat() is more lenient thanNumber() because it ignores trailing invalid characters, which would causeNumber() to returnNaN.
Similar to number literals andNumber(), the number returned fromparseFloat() 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 (seeNumber.MAX_VALUE),-Infinity orInfinity is returned.
Examples
>Using parseFloat()
The following examples all return3.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"; },});parseFloat() returning NaN
The following example returnsNaN:
parseFloat("FF2");Anecdotally, because the stringNaN itself is invalid syntax as accepted byparseFloat(), passing"NaN" returnsNaN as well.
parseFloat("NaN"); // NaNReturning Infinity
Infinity values are returned when the number is outside the double-precision 64-bit IEEE 754-2019 format range:
parseFloat("1.7976931348623159e+308"); // InfinityparseFloat("-1.7976931348623159e+308"); // -InfinityInfinity is also returned when the string starts with"Infinity" or"-Infinity":
parseFloat("Infinity"); // InfinityparseFloat("-Infinity"); // -InfinityInteraction with BigInt values
parseFloat() does not handleBigInt values. It stops at then character, and treats the preceding string as a normal integer, with possible loss of precision. If a BigInt value is passed toparseFloat(), 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); // 900719925474099300parseFloat("900719925474099267n"); // 900719925474099300You should pass the string to theBigInt() function instead, without the trailingn character.
BigInt("900719925474099267");// 900719925474099267nSpecifications
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-parsefloat-string> |