Categories
Web development

JavaScript Math.sign()

JavaScript Math.sign()

The JavaScript Math.sign() returns whether a number is negative, positive or zero.

Syntax:

Math.sign(number)

Note:

If the number is positive, this method returns 1.
If the number is negative, it returns -1.
If the number is zero, it returns 0.
If the number is not a number, it returns NaN.

Example:

<!DOCTYPE html>
<html>
<body>

<p id="example"></p>

<script>
let a = Math.sign(2);
let b = Math.sign(-2);
let c = Math.sign(0);
let d = Math.sign();
  
document.getElementById("example").innerHTML =
a + "<br>" + b + "<br>" + c + "<br>" + d;
</script>

</body>
</html>

Output:

JavaScript Math.sign() returns whether a number is negative, positive or zero:


Note: JavaScript Math.sign() is not supported in Internet Explorer.

Enjoy coding!

Read also:

JavaScript Math Object

JavaScript Math.floor() Method

JavaScript Math Object Properties