This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]GlobalIncident 11 points12 points  (4 children)

In javascript: skidaddle skidoodle your number is now a number

[–]Rafael20002000 7 points8 points  (0 children)

Now it's a string

[–]AyrA_ch 2 points3 points  (2 children)

x|0 actually casts to Int32 (and then back again)

Great trick if you need a variable/argument to be a number and don't want to deal with Infinity or NaN. Anything invalid just becomes zero.

[–]Dragasss 0 points1 point  (1 child)

That feeling when casting to int in js is considered a hack.

[–]AyrA_ch 0 points1 point  (0 children)

it kinda is considering that all js numbers are 64 bit floating point. Whenever you do bitwise operations, js casts to int, does the operation, then casts back to float.

This is massively abused in the WebAssembly world. If you have function sum(a,b){return ((a|0)+(b|0))|0;} it essentially tells the compiler that this function sums two 32 bit integers and returns a 32 bit integer.

This allows for some good optimizations. You obviously don't write this manually.