Is there any trick to convert string to integer in javascript without using any function and methods?
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.
Any help would be appreciated. Thanks in advance.
You can cast the string to number using unary plus (+
). This will do nothing much beside some code optimization.
var s = "5"
s = +s;
console.log(typeof(s), s);
var s = "5.5"
s = +s;
console.log(typeof(s), s);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With