3

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 ?

2
  • Number(x) seems pretty explicit and acceptably terse (compare that to document.getElementById!) to me. Commented Oct 28, 2011 at 20:03
  • I prefer the unary plus because that's its specified purpose: es5.github.com/#x11.4.6 Commented Oct 28, 2011 at 20:06

2 Answers 2

5

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';


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

Sign up to request clarification or add additional context in comments.

5 Comments

lol, true, its not converting to integer, lol. (yet, this does not concern me much, i just want to make sure its not a string) About readability - parseInt imho, decreases it, not improves.
It will become NaN after next calculation, but yes, NaN too. Sometimes you just want numbers to stay numbers.
You did ) Just want to hear other answers, hoping that there is something else, we both haven't thought about. And, regarding your method, - maybe 0|x is better than x|0, readability wise ?
Update: 1326394630086|0 gives -750264378, so i would really discourage anyone from using it.
That's an overflow. Because | only works with 32-bit signed integers which would make the largest positive number possible 2147483647
1

I believe that code should first be correct, then as readable as possible to as many other people as you can and lastly no longer than required. In that vein, here are my preferences:

For conversion from string to integer, I prefer:

parseInt(x, 10)

because I think the code says exactly what you're trying to do. If you don't like the extra parameter, you can define your own global utility function:

toInt(x) {return(parseInt(x, 10));}

so you can just use:

toInt(x)

When I just want to turn a string into a number, I prefer:

Number(x)

because again I think it's the most explicit and the most readable.

In the other examples you have, x.toFixed(0) does not work if x is a string and Number(x), +x and 1*x do not convert to an integer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.