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:
if you want the best readiblilty use parseInt. And the radix is not a hack!
Edit:
My favorite:
Edit: Please do note that this “trick” only works with 32-bit signed integers. Because that’s JavaScript’s implementation of it’s bit logic. So the largest positive number you can use this for is 2147483647.
There is one unsigned bit operation, unsigned right shift.
0 >>> 1