In JavaScript the parseFloat function takes a string as an argument and returns a decimal number.
The syntax is therefore the following: parseFloat(string).
Where string represents the string to convert.
Da zero ad eroe
Entra nel mondo della programmazione web: variabili, funzioni, array, DOM, eventi. Con esercizi pratici e progetti coinvolgenti.
- ⏱️ 8 ore di corso registrato
- 🧩 Più di 50 algoritmi risolti passo passo
- 🎯 Livello base — da zero ad eroe
- 💻 100% online, fruibile quando vuoi
This function, as well as the previous parseInt determines if the first character of the string is a number and continues until it finds a different character.
Let's see some practical examples in this regard.
JavaScript parseFloat - example
Here are some examples with various text strings.
parseFloat("13.50");
parseFloat("13,56")
parseFloat("13:50");
parseFloat("13-01-20");
parseFloat("13 01 20");
parseFloat("13years");
Instead, if we try to pass a string without any initial numeric character, the function returns NaN, as in the following cases:
parseFloat("years13");
parseFloat("thirteen");
Only the first parseFloat will give 13.5, all the others will give us the number 13.
Conlcusion
In this lesson we have studied the JavaScript parseFloat function, in the next lessons I will introduce you to many other useful functions.
Some useful links
JavaScript tutorial
JavaScript Calculator
Object in JavaScript
For in loop
Caesar cipher decoder
Slot Machine in JavaScript
Introduction to JavaScript language
Learn JavaScript – basic concepts
🐍
Vuoi imparare Python da zero?
Scarica la guida gratuita Python in 10 giorni: 10 lezioni pratiche con esempi ed esercizi, direttamente nella tua email.
📥 Scarica gratis il PDF →