Recently I was working on one of my projects and I encountered one scenario in which I had to check that whether the number is positive or negative according to the number’s nature I had to run the logic and then I came to know about Math.sign()
. I should share this thing with you π..
There is a built in fuctinon in javascript Math.sign()
, The Math.sign()
function returns either a positive or negative +/- 1, indicating the sign of a number passed into the argument. If the number passed into Math.sign()
is 0, it will return a +/- 0. Note that if the number is positive, an explicit (+) will not be returned.
Examples
console.log(Math.sign(5));
// expected output: 1
console.log(Math.sign(-5));
// expected output: -1
console.log(Math.sign(0));
// expected output: 0
console.log(Math.sign("-5"));
// expected output: -1
If you find it helpfull please share with your network β .
Happy Coding π !