let theNumber = 123;
console.info(typeof(theNumber)); // number
let theString = theNumber + '';
console.info(typeof(theString)); // string
let theString = '234';
console.info(typeof(theString)); // string
let theNumber = +theString;
console.info(theNumber); // 234
console.info(typeof(theNumber)); // number
- 或使用两个 ~~ 取反操作符也可以快速将字符串转换成数字。
let theString = '234';
console.info(typeof(theString)); // string
let theNumber = ~~theString;
console.info(theNumber); // 234
console.info(typeof(theNumber)); // number