Convert the string to number in JavaScript
In this post we will give you information about Convert the string to number in JavaScript. Hear we will give you detail about Convert the string to number in JavaScriptAnd how to use it also give you demo for it if it is necessary.
we are going to learn how to convert a string to a number in JavaScript with the help of examples.
Consider, we have a following numeric string:
a = "25";
Now, we need to convert the above string to a number like this: “25” to 25 .
Using + Plus Operator
To convert a string to a number, we can use the + operator in JavaScript.
Here is an example:
const a = "25";const result = +a;console.log(result);
Output:
25
In the above code, we have added the + plus operator infront of a string. so that it will converts the string to a number.
Using Number() method
Alternatively, we can also use the built-in Number() method in JavaScript to convert a numeric string into a number.
The Number() method accepts the string as an argument and returns the number.
Here is an example:
const a = "25";const result = Number(a);console.log(result); // 25
Using Number.parseInt() method
The Number.parseInt() takes the two arguments, the first argument is a string and the second argument is the base of a required number that should be converted.
Here is an example:
const a = "25";const result = Number.parseInt(a, 10);console.log(result); // 25
In the above code, we have passed the base 10 as a second argument to the Number.parseInt() method.So it parses the string a returns the of base 10.
Note: Base 10 is used for decimal numbers.
Hope this code and post will helped you for implement Convert the string to number in JavaScript. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs