Convert a String to a Number in JavaScript

This example shows you how to convert a string to a number in JavaScript by using the following global methods:

  • Number()
  • parseInt()
  • parseFloat()
app.js
var numberString1 = '18';
var number1 = Number(numberString1);
console.log(number1, typeof number1); // 18 "number"

var numberString2 = '20';
var number2 = parseInt(numberString2);
console.log(number2, typeof number2); // 20 "number"

var numberString3 = '22.5';
var number3 = parseFloat(numberString3);
console.log(number3, typeof number3); // 22.5 "number"

0 0 vote
Article Rating
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x