Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

JavaScript · Function

parseFloat

parseFloat() converts its argument to a string and parses the longest valid decimal-number prefix. It returns the resulting floating-point number, Infinity, or NaN when parsing cannot begin.

How it works

parseFloat() first converts its argument to a string, ignores leading whitespace, and reads the longest prefix that forms a decimal number. It returns the resulting floating-point value, including Infinity where applicable, or NaN when no valid number begins the input.

Syntax

parseFloat(string)

Parameters

NameTypeDescription
stringanyThe value to parse, coerced to a string. Leading whitespace in this argument is ignored.

Returns

A floating point number parsed from the given string, or NaN when the first non-whitespace character cannot be converted to a number.

Key points

  • The argument is string-coerced before decimal parsing begins.
  • The result is a floating-point Number or NaN when parsing cannot start.
  • Whitespace at the beginning does not affect the parsed value.
  • Its accepted grammar includes signs and decimal digits, with decimal-point, exponent, and Infinity support.
  • Infinity and -Infinity are valid recognized results when they occur at the beginning after optional whitespace.

Common mistakes

  • Parsing a BigInt literal string such as "123n" stops before n and can produce an imprecise number.
  • Inputs that do not begin with a permitted numeric form return NaN.
  • Only one decimal point is accepted, and it must occur before any exponent marker.

Example: Parsing decimal prefixes

const inputs = ["  -12.5px", "3.14e2", "0x10", "Infinity", "oops"];

for (const input of inputs) {
  console.log(input, parseFloat(input));
}

The function trims leading whitespace, reads the longest valid decimal prefix, and returns a number or NaN.

When to use it

Convert textual decimal input

Read decimal values from user input or text fields that may contain surrounding whitespace.

Parse numeric prefixes

Extract a leading numeric measurement from strings such as "12.5px".

Handle exponent notation

Accept decimal scientific notation such as "3.14e2".

Interview questions

QHow does parseFloat() process its argument?

parseFloat(string) converts its argument to a string, trims leading whitespace, and parses the longest valid decimal-number prefix. It returns that number, or NaN if the first non-whitespace character cannot begin a valid number.

Follow-up: How does it differ from Number()? Number() requires the whole converted string to represent a valid number, whereas parseFloat() can stop at trailing invalid characters.

QWhich numeric forms can parseFloat() recognize?

It accepts decimal digits, one decimal point, an optional exponent, an optional leading sign, and the Infinity literal. A sign after an exponent marker is also permitted.

Follow-up: What happens with hexadecimal notation? A string beginning with 0x does not use parseFloat()'s decimal syntax, so it does not produce a hexadecimal value.

QWhat is the BigInt behavior?

parseFloat() does not preserve BigInt precision. A BigInt is converted to a string and parsed as a floating-point number, while a string ending in n is parsed only up to the n character.

Follow-up: Why can precision be lost? JavaScript floating-point numbers cannot represent every integer exactly, so large integer values may be rounded during conversion.

  • Number.parseFloat()
  • parseInt()
  • Number()
  • Number.isNaN()

Source: parseFloat() - JavaScript | MDN. Checked 2026-10-15.