JavaScript Number parseFloat() method - JavaScript Tutorial

JavaScript Number parseFloat() method

BLUF: This tutorial on JavaScript Number parseFloat() method provides an in-depth look at JavaScript's core features. It includes practical examples and code snippets to help you master modern JS development.
Key Discovery: JavaScript Number parseFloat() method

Understanding JavaScript Number parseFloat() method is crucial for building dynamic, interactive web applications. Explore the examples below to see it in action.

The parseFloat function in JavaScript takes a string as its argument and transforms it into a floating point number. If the initial character of the provided value cannot be interpreted as a numerical digit, the function will return NaN.

Syntax

The method parseFloat is expressed using the subsequent syntax:

Example

Number.parseInt(string)

Parameter

string - It represents the string to be parsed.

Return

A floating-point value extracted from the specified input.

JavaScript Number parseFloat method example

In this section, we will explore the parseFloat function by examining several examples.

Example 1

Let us examine a straightforward illustration of the parseFloat function.

Example

<script>

var a="50";

var b="50.25"

var c="String";

var d="50String";

var e="50.25String"

document.writeln(Number.parseFloat(a)+"<br>");

document.writeln(Number.parseFloat(b)+"<br>");

document.writeln(Number.parseFloat(c)+"<br>");

document.writeln(Number.parseFloat(d)+"<br>");

document.writeln(Number.parseFloat(e)); 

</script>

Output:

Output

50

50.25

NaN

50

50.25

Example 2

Let’s examine an illustration that demonstrates how to concatenate two strings both by utilizing and bypassing the parseFloat method.

Example

<script>

var a="10.45";

var b="20.55";

var c=a+b;

document.writeln("Before invoking parseFloat(): "+c+"<br>");

var c=Number.parseFloat(a)+Number.parseFloat(b);

document.writeln("After invoking parseFloat(): "+c);

</script>

Output:

Output

Before invoking parseFloat(): 10.4520.55

After invoking parseFloat(): 31

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience