有没有什么技巧可以在不使用任何函数和方法的情况下在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.Run Code Online (Sandbox Code Playgroud)
任何帮助,将不胜感激。提前致谢。
您可以使用一元加号 ( +)将字符串转换为数字。除了一些代码优化之外,这不会做任何事情。
var s = "5"
s = +s;
console.log(typeof(s), s);
var s = "5.5"
s = +s;
console.log(typeof(s), s);Run Code Online (Sandbox Code Playgroud)
这是在不使用任何内置函数的情况下将字符串转换为 int 的最有效方法,请查看代码。谢谢:)
var s = "5"
var i = s - 0
console.log(typeof(i)) // you will get a number without using any built in function... because (-) will cast string in to numberRun Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1816 次 |
| 最近记录: |