Is there a math function in JavaScript that converts numbers to positive value?
To convert a negative number to a positive one in JavaScript, use the abs() method in JavaScript. The method returns the absolute value of a number.
All you have to do just multiply a negative value with -1 and it will return the positive number instead of the negative.
You could use this...
Math.abs(x)
Math.abs()
| MDN
What about x *= -1
? I like its simplicity.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With