Skip to content

Home

Copy sign to number

Returns the absolute value of the first number, but the sign of the second.

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

More like this

Start typing a keyphrase to see matching snippets.