Convert a String to Number in JavaScript
Last Updated :
15 Oct, 2024
To convert a string to number in JavaScript, various methods such as the Number() function, parseInt(), or parseFloat() can be used. These functions allow to convert string representations of numbers into actual numerical values, enabling arithmetic operations and comparisons.
Below are the approaches to convert a string to a number using JavaScript:
Using '+' operator
The + operator can be operated with the string by using it before the name of the string variable to convert it into a number.
Syntax:
+stringVariable;
Example: The below code implements the + operator to convert a string into a number.
JavaScript
const str = "233";
console.log("Type of str before conversion: ", typeof str);
console.log("Type of str after conversion: ", typeof +str);
OutputType of str before conversion: string
Type of str after conversion: number
Using Number() constructor
The Number() constructor can be used to convert a string into a number by passing the string value as an argument to it.
Syntax:
Number(stringVariable);
Example: The below code converts a string to number with the help of Number() constructor.
JavaScript
const str = "233";
console.log("Type of str before conversion: ", typeof str);
console.log("Type of str after conversion: ", typeof Number(str));
OutputType of str before conversion: string
Type of str after conversion: number
Using parseInt() method
The parseInt() method can be used to convert a numeric string into a number by passing it as an arguement to it. It will always return an integer either you pass a floating point string or integer string.
Syntax:
parseInt(stringVariable);
Example: The below code uses the parseInt() method to convert a string into a number.
JavaScript
const str1 = "233";
const str2 = "23.33"
console.log("Before Conversion: ");
console.log("str1: ", str1, ", Type: ", typeof str1);
console.log("str2: ", str2, ", Type: ", typeof str2);
console.log("After Conversion: ");
console.log("str1: ", parseInt(str1), ", Type: ", typeof parseInt(str1));
console.log("str2: ", parseInt(str2), ", Type: ", typeof parseInt(str2));
OutputBefore Conversion:
str1: 233 , Type: string
str2: 23.33 , Type: string
After Conversion:
str1: 233 , Type: number
str2: 23 , Type: number
Using parseFloat() method
The parseFloat() method can also be used to convert a string into a number in the same way we use the parseInt() method by passing the string value to it. It will return the value as it is as passed to it.
Syntax:
parseFloat(stringVariable);
Example: The below code is practical implementation of the parseFloat() method to convert a string into a number.
JavaScript
const str1 = "233";
const str2 = "23.33"
console.log("Before Conversion: ");
console.log("str1: ", str1, ", Type: ", typeof str1);
console.log("str2: ", str2, ", Type: ", typeof str2);
console.log("After Conversion: ");
console.log("str1: ", parseFloat(str1), ", Type: ", typeof parseFloat(str1));
console.log("str2: ", parseFloat(str2), ", Type: ", typeof parseFloat(str2));
OutputBefore Conversion:
str1: 233 , Type: string
str2: 23.33 , Type: string
After Conversion:
str1: 233 , Type: number
str2: 23.33 , Type: number
Using Math.floor() method
The Math.floor() method will return the low value if the floating point string is passed to it and convert its type from string to number.
Syntax:
Math.floor(stringVariable);
Example: The below code illustrates the use of Math.floo() method to convert a string into a number.
JavaScript
const str1 = "233";
const str2 = "23.33"
console.log("Before Conversion: ");
console.log("str1: ", str1, ", Type: ", typeof str1);
console.log("str2: ", str2, ", Type: ", typeof str2);
console.log("After Conversion: ");
console.log("str1: ", Math.floor(str1), ", Type: ", typeof Math.floor(str1));
console.log("str2: ", Math.floor(str2), ", Type: ", typeof Math.floor(str2));
OutputBefore Conversion:
str1: 233 , Type: string
str2: 23.33 , Type: string
After Conversion:
str1: 233 , Type: number
str2: 23 , Type: number
Using Math.round() method
The Math.round() method will round up the passed floating string and returns its value after rounding it up. It also converts the type of string to number.
Syntax:
Math.round(stringVariable);
Example: The below code example uses the Math.round() method to convert a string into a number.
JavaScript
const str1 = "233";
const str2 = "23.33"
console.log("Before Conversion: ");
console.log("str1: ", str1, ", Type: ", typeof str1);
console.log("str2: ", str2, ", Type: ", typeof str2);
console.log("After Conversion: ");
console.log("str1: ", Math.round(str1), ", Type: ", typeof Math.round(str1));
console.log("str2: ", Math.round(str2), ", Type: ", typeof Math.round(str2));
OutputBefore Conversion:
str1: 233 , Type: string
str2: 23.33 , Type: string
After Conversion:
str1: 233 , Type: number
str2: 23 , Type: number
Similar Reads
Convert a Number to a String in JavaScript These are the following ways to Convert a number to a string in JavaScript:1. Using toString() Method (Efficient and Simple Method)This method belongs to the Number.Prototype object. It takes an integer or a floating-point number and converts it into a string type.JavaScriptlet a = 20; console.log(a
1 min read
Convert Array to String in JavaScript In JavaScript, converting an array to a string involves combining its elements into a single text output, often separated by a specified delimiter. This is useful for displaying array contents in a readable format or when storing data as a single string. The process can be customized to use differen
7 min read
JavaScript - Convert String to Array Strings in JavaScript are immutable (cannot be changed directly). However, arrays are mutable, allowing you to perform operations such as adding, removing, or modifying elements. Converting a string to an array makes it easier to:Access individual characters or substrings.Perform array operations su
5 min read
Convert a String to an Integer in JavaScript These are the following ways to convert string to an integer in JavaScript:1. Using Number() MethodThe number() method converts a string into an integer number. It works similarly to the unary plus operator. JavaScriptlet s = "100.45"; console.log(typeof Number(s));Outputnumber 2. Using Unary + Oper
2 min read
How to format numbers as currency string in JavaScript ? A number, represented as monetary value, creates an impact and becomes much more readable, and that's the reason behind formatting a number as currency. For example, a number, let's say 100000 when represented as $100,000.00 it becomes pretty much understood that it represents a monetary value, and
3 min read
How to convert a String into Number in PHP ? Strings in PHP can be converted to numbers (float/ int/ double) very easily. In most use cases, it won't be required since PHP does implicit type conversion. This article covers all the different approaches for converting a string into a number in PHP, along with their basic illustrations.There are
4 min read
How to Validate Number String in JavaScript ? Validating a number string in JavaScript involves ensuring that a given string represents a valid number. This typically includes checking for digits, optional signs (+/-), decimal points, and possibly exponent notation (e.g., "1.23e4"). We will use various methods to validate number strings in Java
2 min read
How to convert long number into abbreviated string in JavaScript ? We are given a long number and the task is to convert it to the abbreviated string(eg.. 1234 to 1.2k). Here 2 approaches are discussed with the help of JavaScript.Approaches to Convert Long Number to Abbreviated String:Table of ContentUsing JavaScript methodsUsing Custom functionUsing logarithmsUsin
6 min read
JavaScript Program to Convert Date to Number This article will explain how to convert a Date into a Number using JavaScript. The JavaScript Date object is used to get dates and times. It helps in formatting the dates. We can use the new keyword to create an object for Date. To convert a Date to a Number, we use the getTime() method. getTime()
2 min read
How to convert array of strings to array of numbers in JavaScript ? Converting an array of strings to an array of numbers in JavaScript involves transforming each string element into a numerical value. This process ensures that string representations of numbers become usable for mathematical operations, improving data handling and computational accuracy in applicati
4 min read