In JavaScript, you can convert a positive number to negative in the following ways:
If you wish to always return a negative number (i.e. retain the sign of a negative number whilst ensuring that any positive number is converted to negative), then you can first convert the number to its absolute form, and then negate it.
#Using Unary Negation Operator
You can convert a positive number to negative by using the unary negation operator (-) like so:
-n
For example, you can use this in the following way:
const num = 1234;
const negated = -num;
console.log(negated); // -1234
#Using Arithmetic Operators
You can convert a positive number to negative by simply multiplying or dividing it by -1:
n * -1
n / -1
Alternatively, you may subtract the number from 0:
0 - n
You can use any of these to convert a positive number to a negative number, for example, like so:
const num = 1234;
const negated = num * -1;
console.log(negated); // -1234
const num = 1234;
const negated = num / -1;
console.log(negated); // -1234
const num = 1234;
const negated = 0 - num;
console.log(negated); // -1234