⚖️(~~) vs. Math.trunc()

xJSstatementexpressionoperator ⟩ double tilde (~~)

nearest integer towards 0.

// ⭐️ ~~(x) : nearest integer towards 0
//    (almost) same as Math.trunc() = sign(x) * [|x|]
~~(7.8),            //  7
~~(-5.6),           // -5
Math.trunc(7.8),    //  7
Math.trunc(-5.6),   // -5

~~M,                // ❗ 0 (overflow: ~~(x+M) = ~~x)
Math.trunc(M),      // ⭐️ M (Math.trunc() has wider range)

👉 bitwise not (~) | 🌟 2's complement

~~ vs. Math.trunc():the difference

Last updated