If you have some JavaScript value in x and you want it as a 32-bit signed integer then you can use the expression:
x|0
or change x using the assignment expression:
x|=0
This leaves 32-bit signed integers untouched, and does plausible things for fractional values, very large numbers, strings that look like numbers, true
and false
, NaN
, infinities, and any other objects. It can’t raise an exception. It’s very short.
I can’t remember what I was trying to do when I discovered this, so I haven’t got a good use case…
Leave a comment