Converting Non-numeric values to Numbers
suggest change~~ applies bitwise NOT twice. The value is converted with ToNumber, then ToInt32, which truncates toward zero into a 32-bit integer. It does not negate the value: ~~"1" is 1.
If ToNumber yields NaN, the result is 0. That includes non-numeric strings such as "true". true converts to 1 and false to 0 because those are their numeric values, not a special case of ~~. Values outside the 32-bit signed range wrap.
let a = ~~"-2"; // a is now -2
let b = ~~"1"; // b is now 1
let c = ~~"0"; // c is now 0
let d = ~~"true"; // d is now 0
let e = ~~"false"; // e is now 0
let f = ~~true; // f is now 1
let g = ~~false; // g is now 0
let h = ~~""; // h is now 0
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents