Articles by FavTutor
  • AI News
  • Data Structures
  • Web Developement
  • AI Code GeneratorNEW
  • Student Help
  • Main Website
No Result
View All Result
FavTutor
  • AI News
  • Data Structures
  • Web Developement
  • AI Code GeneratorNEW
  • Student Help
  • Main Website
No Result
View All Result
Articles by FavTutor
No Result
View All Result
Home Web Developement

Convert String to Float in JavaScript (5 Easy Ways)

Komal Bhatia by Komal Bhatia
November 30, 2023
Reading Time: 6 mins read
Convert string to float in JavaScript
Follow us on Google News   Subscribe to our newsletter

A string is nothing but a sequence of characters put together. However, some problems require the conversion of string data type to some other data type. In this article, we will look into Strings in JavaScript and the techniques to convert a String to Float in JavaScript. Let us get started!

How to Convert String to Float in JavaScript?

Strings in JavaScript represent a sequence of multiple characters. ‘C’ represents a single character and “Cat” represents a string. In JavaScript, we can declare a string in two ways.

There are various techniques that help us convert a JavaScript String to Float. We will discuss 5 different methods with examples.

1) Using parseFloat() Functions

The easiest method to convert a string to a float number is by using the parseFloat() function. The parseFloat() function returns a float or NaN. 

The syntax of parseFloat() is as follows:

let num = parseFloat(value);

where ‘value’ can be string, float, etc. 

Let us see an example:

//declaring a variable which is a string
let strFloat="13.65";

//printing the "strFloat" variable and it's type
console.log(strFloat + " " + typeof(strFloat));

//using the parseFloat() function
let convertedNumFloat=parseFloat(strFloat);

//printing the "convertedNumFloat" variable and it's type
console.log(convertedNumFloat + " " + typeof(convertedNumFloat));

Output:

13.65 string
13.65 number

In this example, we have first taken a strFloat variable to store a string equal to “13.65”. We checked the type of the variable using the “typeof()” function. We used the parseFloat() function to convert the string to a float number. We finally check the “convertedNumFloat” variable type to verify whether the Number() function has converted the string to a float number. 

We have got the desired output as a float.

2) Using Number() Function

Another method is the simple “Number()” function. The syntax of the Number()  function is as follows:

let num = Number(value);

where, 

num: a variable to store the number we get after passing any value as a parameter

value: can be a string, float, etc. 

Here’s an example of using the Number() Function to convert string to float:

//declaring a variable which is a string
let str="15.7";

//printing the "str" variable and it's type
console.log(str + " " + typeof(str));

//using the Number() function
let num=Number(str);

//printing the "num" variable and it's type
console.log(num + " " + typeof(num));

Output:

15.7 string
15.7 number

In this example, we have first taken a str variable to store a string equal to “15.7”. We checked the type of the variable using the “typeof()” function. We used the Number() function to convert the string to a float number. We finally check the type of the “num” variable to verify whether the Number() function has converted the string to a float number. We have obtained the desired result.

3) Using the Unary Plus Operator

In JavaScript, the unary plus operator (+) is a simple and concise way to convert a string to a float number. Adding the “+” operator before a string in JavaScript automatically converts the string to a numeric value (type conversion).

This method works for the float values in the following way:

//declaring a variable which is a string
let strFloat="13.65";

//printing the "strFloat" variable and it's type
console.log(strFloat + " " + typeof(strFloat));

//converting string to number using unary operator
let convertedNumFloat=+strFloat;

//printing the "convertedNumFloat" variable and it's type
console.log(convertedNumFloat + " " + typeof(convertedNumFloat));

Output:

13.65 string
13.65 number

The string “13.65” has been converted to a float number 13.65.

4) Subtracting the Number 0 from the String

The next approach to convert a string to a float number is by subtracting 0 from the string. JavaScript automatically performs a type conversion while subtracting 0 from the string, converting a string to a number.

Let us see an example:

//declaring a variable which is a string
let strFloat="13.65";

//printing the "strFloat" variable and its type
console.log(strFloat + " " + typeof(strFloat));

//converting string to number by subtracting 0 from it
let convertedNumFloat=strFloat-0;

//printing the "convertedNumFloat" variable and its type
console.log(convertedNumFloat + " " + typeof(convertedNumFloat));

Output:

13.65 string
13.65 number

We have obtained the desired output.

5) Multiplying and Dividing the String by 1

The best approach to convert a string into a float number is by multiplying or dividing the string value by 1. JavaScript automatically performs a type conversion, converting the string into a float number.

Here’s an example of converting a float string number to an actual float number by multiplying it by 1.

//declaring a variable which is a string
let strFloat="13.45";

//printing the "strFloat" variable and its type
console.log(strFloat + " " + typeof(strFloat));

//converting string to number by multiplying with 1
let convertedNumFloat=strFloat*1;

//printing the "convertedNum" variable and its type
console.log(convertedNumFloat + " " + typeof(convertedNumFloat));

Output:

13.45 string
13.45 number

Here’s an example of converting a float string to an actual float number by dividing it by 1:

//declaring a variable which is a string
let strFloat="13.45";

//printing the "strFloat" variable and its type
console.log(strFloat + " " + typeof(strFloat));

//converting string to number by dividing with 1
let convertedNumFloat=strFloat/1;

//printing the "convertedNum" variable and its type
console.log(convertedNumFloat + " " + typeof(convertedNumFloat));

Output:

13.45 string
13.45 number

We have obtained the desired result.

These were some of the methods to convert a string to a float. You can also learn how to convert string to array in an easier way.

Conclusion

In this article, we discussed various JavaScript techniques to convert String to Float data type It is essential to know at least three of the methods when working in JavaScript as they come in very handy at times.

ShareTweetShareSendSend
Komal Bhatia

Komal Bhatia

I am a dedicated Computer Science student with a strong track record of academic excellence. I am a highly motivated individual with a passion for providing creative and innovative solutions to complex problems. I possess skills in the domain of C++ and web development and am always eager to contribute to the field of Software Development.

RelatedPosts

Javascript Animation Libraries

Top 10 JavaScript Animation Libraries in 2025

February 19, 2025
JavaScript Interview Questions

Top 40 JavaScript Interview Questions and Answers in 2024

April 1, 2025
Best React UI Component Libraries

10 Must-Know React UI Component Libraries for Web Devs 2024

May 7, 2024
Currying in JavaScript

Currying in JavaScript Explained (with Examples)

March 15, 2024
Javascript Format Currency

Javascript Program for Format Currency

April 8, 2025

About FavTutor

FavTutor is a trusted online tutoring service to connects students with expert tutors to provide guidance on Computer Science subjects like Java, Python, C, C++, SQL, Data Science, Statistics, etc.

Categories

  • AI News, Research & Latest Updates
  • Trending
  • Data Structures
  • Web Developement
  • Data Science

Important Subjects

  • Python Assignment Help
  • C++ Help
  • R Programming Help
  • Java Homework Help
  • Programming Help

Resources

  • About Us
  • Contact Us
  • Editorial Policy
  • Privacy Policy
  • Terms and Conditions

Website listed on Ecomswap. © Copyright 2025 All Rights Reserved.

No Result
View All Result
  • AI News
  • Data Structures
  • Web Developement
  • AI Code Generator
  • Student Help
  • Main Website

Website listed on Ecomswap. © Copyright 2025 All Rights Reserved.