Math.sign()
to check if the two numbers have the same sign.x
if they do, -x
otherwise.const copySign = (x, y) => Math.sign(x) === Math.sign(y) ? x : -x;
copySign(2, 3); // 2
copySign(2, -3); // -2
copySign(-2, 3); // 2
copySign(-2, -3); // -2
Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️