How to change the sign of a number in JS
There comes a situation where we need to change a number's sign (+/-). We can do this in two ways:
abs gives the absolute number irrespective of a number's sign.
const randomDiff = Math.abs(Math.random() - Math.random())console.log(randomDiff)
On the other hand,
sign returns the sign of a number as either
0 if the number is
const randomNumber = Math.random() - Math.random()const randomDiff = Math.sign(randomNumber) * randomNumberconsole.log(randomDiff)