Is there any way to convert a variable from string to number?
For example, I have
var str = "1";
Can I change it to
var str = 1;
Is there any way to convert a variable from string to number?
For example, I have
var str = "1";
Can I change it to
var str = 1;
Try str = parseInt( str, 10 )
(note: the second parameter signifies the radix for parsing; in this case, your number will be parsed as a decimal)
That being said, however, the term "type" is used quite loosely in JavaScript. Could you explain why are you concerned with variable's type at all?
There are three ways to do this:
str = parseInt(str, 10); // 10 as the radix sets parseInt to expect decimals
or
str = new Number(str); // this does not support octals
or
str = +str; // the + operator causes the parser to convert using Number
Choose wisely :)