⚖️(~~) vs. Math.trunc()
xJS ⟩ statement ⟩ expression ⟩ operator ⟩ 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)
~~ vs. Math.trunc():the difference
will try to convert x with "prefer-number" conversion, if it's NaN, the result remains NaN.
has wider range. (see example above)
Last updated
Was this helpful?