I was looking at the generated JS code and noticed...
# javascript
e
I was looking at the generated JS code and noticed there are a lot of
| 0
, especially when incrementing numbers in loops. What's the theory behind that? For reference, the Kotlin code is:
Copy code
for (c in this) { // this is a String
  val code = c.code
  ...
}
j
It tells the VM that it's an integer and not a floating-point value
It's been around forever, but it gained huge traction with tooling that produces JS as a result of asm.js https://en.wikipedia.org/wiki/Asm.js
e
Oh cool! Thanks. So it's mostly used by code generation tools? (asking because I've never seen it used in code written by a human, at least not for TypeScript stuff)
j
Unless you were optimizing for extreme performance, a human would be unlikely to write such code.
gratitude thank you 1
a
Also, in typescript there is no difference between integer and floating-point numbers, so, they don't add anything like this. You could also notice that for integer multiplication we use
Math.imul
instead of the regular
*
✔️ 1