Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

parseFloat — reading a number from the start of text

parseFloat converts its argument to text and reads the longest valid prefix in decimal numeric notation. It ignores leading whitespace and trailing text; when no number can be read, it returns NaN.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

parseFloat(string)

parseFloat(string) is a global ECMAScript function, not a browser API. It first converts its argument to text, skips leading whitespace, and reads the longest valid numeric prefix. It returns a Number value; Number.parseFloat refers to the same function.

It recognizes an optional + or - sign, a decimal point, an e or E exponent, and Infinity. Remaining text is ignored: parseFloat("12.5px") returns 12.5, and parseFloat("1e") returns 1. A comma is not a decimal separator: parseFloat("3,14") returns 3. Hexadecimal prefixes are not supported: parseFloat("0x10") returns 0.

Empty text or the absence of a valid numeric prefix produces NaN. Test the result with Number.isNaN(result), not result === NaN. A successfully parsed number can be infinite or rounded to Number precision; Number.isFinite(result) rejects both NaN and infinities.

Do not use this function to validate an entire numeric field: it also accepts "12abc". Number(string) requires the entire text to be valid after surrounding whitespace is ignored, but converts empty text to 0. parseFloat does not accept a radix argument.

Examples

const inputs = ["  -12.5px", "1.25e2kg", "3,14", "0x10", "abc", ""];
for (const input of inputs) {
  console.log(`${JSON.stringify(input)} => ${parseFloat(input)}`);
}

Result

"  -12.5px" => -12.5
"1.25e2kg" => 125
"3,14" => 3
"0x10" => 0
"abc" => NaN
"" => NaN

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources