by Sai gowtham

Convert the string to number in JavaScript

In this tutorial, 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.

Css Tutorials & Demos

How rotate an image continuously in CSS

In this demo, we are going to learn about how to rotate an image continuously using the css animations.

How to create a Instagram login Page

In this demo, i will show you how to create a instagram login page using html and css.

How to create a pulse animation in CSS

In this demo, i will show you how to create a pulse animation using css.

Creating a snowfall animation using css and JavaScript

In this demo, i will show you how to create a snow fall animation using css and JavaScript.

Top Udemy Courses

JavaScript - The Complete Guide 2022 (Beginner + Advanced)
JavaScript - The Complete Guide 2022 (Beginner + Advanced)
116,648 students enrolled
52 hours of video content
$14.99 FROM UDEMY
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
631,582 students enrolled
49 hours of video content
$24.99 FROM UDEMY
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
203,937 students enrolled
31.5 hours of video content
$14.99 FROM UDEMY