JavaScript parseFloat() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Global function

Definition

parseFloat() reads text from left to right and gives you back a number that may have a decimal part. It stops at the first character that cannot be part of the number, so "3.14abc" becomes 3.14.

It is the decimal version of parseInt(). If the text does not start with a number, you get NaN ("not a number").

Remember
parseFloat(string);

string  -> the text to read (spaces at the start are skipped)
returns -> a number (it can have decimals), or NaN

parseFloat has no radix: it always reads base 10.
It stops at the first character that does not fit.

Syntax

Call parseFloat() by name and give it the text. There is only one argument.

JavaScript
parseFloat(string);
CallWhat it does
parseFloat("3.14abc")Reads 3.14 and stops at "a". Result: 3.14.
parseFloat(" 2.5kg")Skips the spaces and reads 2.5. Result: 2.5.
parseFloat("1e3")Understands the exponent form. Result: 1000.
parseFloat("price")The text does not start with a number. Result: NaN.

Built into JavaScript (ES1). It is a global function, so you call it by name with no import. Number.parseFloat is the very same function. See also: parseInt(), isNaN() and isFinite().

Library declarations
JavaScript
// Global function (ES1)
parseFloat(string)

// returns a number or NaN
// Number.parseFloat === parseFloat   // true

Parameters

ParameterDescription
stringThe text to read. A value that is not text is converted to text first. Spaces, tabs and line breaks at the start are skipped. An optional + or - sign, one decimal point and an exponent such as e3 are allowed. Infinity is read too.

Return Value

Return ValueDescription
A numberThe number found at the start of the text. Anything after the first character that does not fit is ignored, so "1.2.3" gives 1.2.
NaNThe text (after any spaces and sign) does not start with a digit or a decimal point, or the text is empty.

JavaScript Examples

Example 1 — Read a Decimal Number from Text

See which texts parseFloat() understands and where it stops.

JavaScript
console.log("3.14 -> " + parseFloat("3.14"));
console.log("3.14abc -> " + parseFloat("3.14abc"));
console.log("  2.5kg -> " + parseFloat("  2.5kg"));
console.log(".5 -> " + parseFloat(".5"));
console.log("-0.75 -> " + parseFloat("-0.75"));
console.log("1e3 -> " + parseFloat("1e3"));
console.log("1.2.3 -> " + parseFloat("1.2.3"));
console.log("abc -> " + parseFloat("abc"));
Try It Yourself

How It Works

  1. Decimals are kept. "3.14" gives 3.14, and a number that starts with a dot, ".5", gives 0.5.
  2. Extra text is ignored. "3.14abc" and " 2.5kg" work, because reading stops at the first letter and spaces at the start are skipped.
  3. Exponents work. "1e3" means 1 times 10 to the power 3, which is 1000.
  4. Only one dot. In "1.2.3" the second dot stops the reading, so the result is 1.2.
  5. No number, no result. "abc" does not start with a digit, so the result is NaN.

Example 2 — Clean Up Text Before Reading It

Text from a page or a form often has units, symbols or a comma. Read the number, then use it in a calculation.

JavaScript
const widthText = "250.5px";
const width = parseFloat(widthText);
console.log("Width: " + width);
console.log("Doubled: " + width * 2);

const price = "$19.99";
console.log("Direct: " + parseFloat(price));
console.log("After removing the $: " + parseFloat(price.slice(1)));

console.log("Comma: " + parseFloat("1,5"));
console.log("Comma replaced: " + parseFloat("1,5".replace(",", ".")));
Try It Yourself

How It Works

  1. Units are fine. "250.5px" gives 250.5, a real number you can multiply. Without parseFloat() you would be adding and multiplying text.
  2. A leading symbol is not. "$19.99" starts with $, so parseFloat() returns NaN. Cut the symbol off with slice(1) and try again.
  3. Commas stop the reading. "1,5" gives 1. Use replace(",", ".") to turn the decimal comma into a dot first.

Example 3 — Compare parseFloat(), Number() and parseInt()

Three ways to turn text into a number, tried on the same five texts.

JavaScript
const samples = ["42", "42.9", "42abc", "", "0x1F"];

samples.forEach(function (text) {
  const line = "'" + text + "' -> parseFloat: " + parseFloat(text) +
    ", Number: " + Number(text) +
    ", parseInt: " + parseInt(text, 10);
  console.log(line);
});
Try It Yourself

How It Works

  1. Same on clean numbers. For "42" all three agree. For "42.9" only parseInt() cuts off the decimal part.
  2. Strict or forgiving. Number("42abc") is NaN because the whole text must be a number. parseFloat() and parseInt() read 42 and ignore the letters.
  3. Empty text. Number("") is 0, but parseFloat("") is NaN. Check for empty text before converting.
  4. Hexadecimal. Number("0x1F") understands the 0x prefix and gives 31. parseFloat() and parseInt(text, 10) read only the 0.

Browser & Runtime Support

parseFloat() is one of the oldest functions in JavaScript. Every browser and every Node.js version has it.

✓ ES1 · Works everywhere

parseFloat()

Supported in all browsers, including old ones, and in Node.js, Deno and Bun.

ES1 Standard since
Google Chrome All versions · Desktop & Mobile
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Opera All versions
Full support
Samsung Internet All versions · Android
Full support
Android WebView All versions
Full support
Node.js All versions · Server runtime
Supported
Deno All versions · JavaScript runtime
Supported
Bun All versions · JavaScript runtime
Supported
Internet Explorer All versions
Full support
parseFloat() Excellent

Bottom line: Safe to use anywhere. Only the dot is read as a decimal mark in every browser and language setting, so clean up text that uses a comma.

Frequently Asked Questions

It reads a number, with an optional decimal part, from the start of a string and returns it as a number. It stops at the first character that does not fit, so parseFloat("3.14abc") returns 3.14. If the string does not start with a number, the result is NaN.
parseFloat() keeps the decimal part and reads exponents, while parseInt() reads only whole digits and cuts the decimals off. parseInt() also has a radix argument for other number bases; parseFloat() always uses base 10.
parseFloat() ignores everything after the number, so parseFloat("12px") is 12. Number() needs the whole text to be a number, so Number("12px") is NaN. Number("") is 0, but parseFloat("") is NaN.
parseFloat() only understands the dot as the decimal mark. It stops reading at the comma, so only 1 is left. Replace the comma first: parseFloat("1,5".replace(",", ".")) returns 1.5.
That comes from how computers store decimal numbers, not from parseFloat(). It happens in every language that uses floating point. Round the result for display, for example with toFixed(2), and keep money in whole cents when you can.

Next: parseInt()

Learn how to read whole numbers from text, in base 10, binary, hexadecimal and more.

parseInt() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful