Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Another way to cast to a Number is the bitwise-or operator. It has the useful property of always yielding a number.

    '42' | 0;      // 42
    NaN | 0;       // 0
    null | 0;      // 0
    undefined | 0; // 0
    false | 0;     // 0
    true | 0;      // 1


Bitwise operations don't produce Numbers (f64), but signed integers (i32).


> produce...signed integers

Well, except for right logical shifts.


It always yields an integer, so it's useful if you know you want an integer.




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

Search: