Hacker News new | past | comments | ask | show | jobs | submit login

>Not in the JS specification (outside typed arrays), but every JS JIT works in a way you can actually declare variables as 32 bit signed integers, and made its way into the asm.js specification. They are declared like this:

The "|0" trick you mention is not for javascript; it is for asm.js; to be able to declare such a "true integer" variable, your code would need to be in asm.js, not javascript.

Javascript has no integers, only floating point numbers. This is a very strong limitation.




What would entail to declare a "true integer"?

Correctness? The |0 after each operation makes it correct. All bitwise operations in JS operate on signed 32 bit integers.

Speed? All JS JITs have optimizations for integers, and it's the reason asm.js uses that trick, not the other way around.

Precision? If you don't use bitwise operations, you have up to 53 bits of perfect integer precision, plus bit sign. Guaranteed by the standard.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: