在不使用任何函数和方法的情况下,是否有任何技巧可以在 javascript 中将字符串转换为整数?
var s = "5"
console.log(typeof(s)) // out put is string
console.log(typeof(parseInt(s))) // i want out put which is number with out using parseInt() or other functions for optimizing code.
如有任何帮助,我们将不胜感激。提前致谢。
最佳答案
您可以使用一元加号 (+
) 将字符串转换为数字。除了一些代码优化之外,这不会做太多事情。
var s = "5"
s = +s;
console.log(typeof(s), s);
var s = "5.5"
s = +s;
console.log(typeof(s), s);
关于javascript - 在不使用任何内置函数的情况下在javascript中将String转换为整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54920220/