How get value numerals from x= "11" that was x=11 with help JavaScript | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How get value numerals from x= "11" that was x=11 with help JavaScript

14th Aug 2017, 5:38 AM
Alex Polev
Alex Polev - avatar
2 Answers
+ 2
I believe you are asking how to cast a string datatype into an integer Javascript has a built in function called parseInt which allows you to do this Example: var x = '11'; var y = parseInt(x); y now equals 11
14th Aug 2017, 5:46 AM
S C
+ 9
You can use a regex to get the first integer : var num = parseInt(str.match(/\d+/),10) If you want to parse any number (not just a positive integer, for example "asd -98.43") use var num = str.match(/-?\d+\.?\d*/) Now suppose you have more than one integer in your string : var str = "a24b30c90"; Then you can get an array with var numbers = str.match(/\d+/g).map(Number); Result : [24, 30, 90]
14th Aug 2017, 5:47 AM
Apoorva Shenoy Nayak
Apoorva Shenoy Nayak - avatar