how do I convert string "01" to number in JavaScript? -


i'd know if possible use 2 digits in number in javascript if number less 10?

for example

10,9,8,7,6 , on 10,09,08,07,06 "number data type"

yes it's possible display numbers in 2 digits when numbers less 10 "string data type".

if(number < 10) {     console.log(number) //number     number = "0" + number;     console.log(number) //string } 

but want use number data type can use

if(number == 01) {     //some code here } 

is possible?

parseint syntax

reference

parseint(string, radix);

parameters

string value parse. if string not string, converted string (using tostring abstract operation). leading whitespace in string ignored.

radix integer between 2 , 36 represents radix (the base in mathematical numeral systems) of above mentioned string. specify 10 decimal numeral system commonly used humans. specify parameter eliminate reader confusion , guarantee predictable behavior. different implementations produce different results when radix not specified, defaulting value 10.

do not do

var number = "09"; if(number == 09) { // here not compare type check == , ===     alert("ok: " + number) } else {     alert("problem: " + number); } 

correct answer

var number = "09"; var decimal = parseint(number,10); if(decimal === 09) {     alert("ok: " + decimal) } else {     alert("problem: " + decimal); } 

check in console

var first = 10 ; var secn = "10";  first == secn // true because both equal.  first === secn // both not equal type(string , number)    var result = parseint("010", 10) == 10; // returns true  var result = parseint("010") == 10; // returns false 

Comments