# JavaScript Convert String to Number

In this tutorial, You will learn how to convert string to number in javascript. Learn various methods of javascript with examples to convert strings to numbers in javascript.

If you work with strings in javascript, you should also read following below javascript string related posts:

## JavaScript Convert String to Number

This tutorial purpose is to explain and share the best ways to convert string into numbers in javascript.

### 1: First Method – parseInt()

You can use the javascript parseInt() method to convert the string to a number.

Note:- The javascript parseInt () function returns an integer number by parse the string. If the first character can not be changed in number, the function returns NaN.

#### Syntax:-

`parseInt(string, radix)`

#### Ex:-

```    // returns a Integer no
a = parseInt("20");
document.write('parseInt("20") = ' + a + "<br>");
//  Not a Number character
b = parseInt("30.00");
document.write('parseInt("30.00") = ' + b + "<br>");

// returns NaN on Non numeral character
c = parseInt("2.366");
document.write('parseInt("2.366") = ' + c + "<br>");

// returns Integer value of a Floating point Number
d = parseInt("3.14");
document.write('parseInt("3.14") = ' + d + "<br>");

// only first Number it encounters
e = parseInt("22 3 2019");
document.write('parseInt("22 3 2019") = ' + e + "<br>");
```

Result of the above code is:-

```parseInt("20") = 20
parseInt("30.00") = 30
parseInt("2.366") = 2
parseInt("3.14") = 3
parseInt("22 3 2019") = 22 ```

### 2: Second Method – parseFloat()

You can use the javascript parseFloat() method, which is to convert the string into a floating number or a decimal number

Note:- The ParseFloat () Javascript has an inbuilt function.
ParseFloat has accepted the string and convert it to a floating-point number.

#### Syntax:-

`parseFloat(string)`

#### Ex:-

``` // return float value
a = parseFloat("  10  ")
document.write('parseFloat("  10  ") = ' +a +"<br>");

b = parseFloat("123abc")
document.write('parseFloat("123abc") = '+b +"<br>");

// returns NaN value
c = parseFloat("abc456")
document.write('parseFloat("abc456") = ' +c +"<br>");

d = parseFloat("3.14")
document.write('parseFloat("3.14") = '+d +"<br>");

// returns only first Number
e = parseFloat("18 2 2019")
document.write('parseFloat("18 2 2019") = ' +e +"<br>");
```

Result of the above code is:

`parseFloat(" 10 ") = 10parseFloat("123abc") = 123parseFloat("abc456") = NaNparseFloat("3.14") = 3.14parseFloat("18 2 2019") = 18 `

### 3: Third Method – Number()

You can also use the javascript number() method to convert the string into number in javascript.

Note:- If you want to pass string with a character or text, it can not be changed in number, the function returns NaN.

#### Syntax:-

`Number(string)`

#### Ex:-

```// return float value
a = Number("  10  ")
document.write('Number("  10  ") = ' +a +"<br>");

b = Number("125.3")
document.write('Number("125.3") = '+b +"<br>");

// returns NaN value
c = Number("abc456")
document.write('Number("abc456") = ' +c +"<br>");

d = Number("42px")
document.write('Number("42px") = '+d +"<br>");
```

Result of the above code is:

Number(” 10 “) = 10
Number(“125.3”) = 125.3
Number(“abc456”) = NaN
Number(“42px”) = NaN

If you want to know more javascript string methods, you may like