if you compare different explicit methods of type-casting a variable to integer:
var y = parseInt(x,10) + 'text'; // too long, needs wrapping, needs anti-octal hack
var y = x.toFixed(0) + 'text'; // still long, and even uglier, and maybe buggy
var y = Math.floor(x) + 'text'; // long and uses Math object
var y = Number(x) + 'text'; // long
var y = +x + 'text'; // very short, but too hacky
var y = 1 * x + 'text'; // simple and short
You will see, why the last one is my favourite. Yet, i wonder, if there are any hidden issues with this method ?
The last one does work:
1 * 0.5; // 0.5
if you want the best readiblilty use parseInt. And the radix is not a hack!
Edit:
My favorite:
var y = x|0 + 'text';
There is one unsigned bit operation, unsigned right shift. 0 >>> 1
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