# Copy sign from one number to another in JavaScript

If you've ever worked extensively with numeric values, you might have stumbled upon a case where you need to change the sign of a number. This is relatively simple, but what if you want to copy the sign from another value. Naively, most of us would simply add a ternary operator and check if the second number is positive or negative, then do the same for the first number. But there's a much simpler way to do this.

In the simplest terms possible, either the target value has the correct sign (same as the source value) or it doesn't. All you really need to compare are the signs of the two numbers. If they're the same, you're done. If they're different, you just need to change the sign of the target value.

So how can that be done? Luckily, `Math.sign()` can help us out here. This methods returns either `1` (positive), `0` (zero), or `-1` (negative) depending on the sign of the number. By comparing the signs of the two numbers, we can easily determine if the sign needs to be changed.

The only thing left to do is use the ternary operator (`?`) to conditionally apply the unary `-` operator to the target value. If the signs are the same, the target value remains unchanged. If they're different, the sign is flipped.

```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

• JavaScript ·

### How can I remove trailing zeros from a number in JavaScript?

When formatting decimal values in JavaScript, trailing zeros can be undesired. Here's how to deal with them.

• JavaScript ·

### Formatting numeric values in JavaScript

Learn common number formatting operations, such as rounding, padding, optional decimal marks, currency, seconds, bytes, and more.

• JavaScript ·

### Clamp or map a number to a range in JavaScript

Clamping and mapping a number to a range are two common and easily confused operations. Learn how to perform each in JavaScript.

• JavaScript ·

### Validate a number in JavaScript

Check if a value can be safely converted to a number in JavaScript.

Start typing a keyphrase to see matching snippets.