JavaScript Math sign() method

The JavaScript Math.sign function provides the sign of a specified number. It reveals whether the input number is positive, negative, or equals zero.

Syntax

The syntax for the sign method is expressed as follows:

Example

Math.sign(num)

Parameter

num - A number.

Return

The sign of the given number.

JavaScript Math sign method example

In this section, we will explore the sign method by examining a range of examples.

Example 1

Let’s examine an illustration to determine the sign of the specified number.

Example

<script>

document.writeln(Math.sign(12)+"<br>");

document.writeln(Math.sign(-12)+"<br>");

document.writeln(Math.sign(0));

</script>

Output:

Output

1

-1

0

Example 2

Let’s examine a few scenarios in which the sign function yields NaN.

Example

<script>

document.writeln(Math.sign(NaN)+"<br>");

document.writeln(Math.sign("string")+"<br>");

document.writeln(Math.sign());

</script>

Output:

Output

NaN

NaN

NaN

Example 3

In this section, you have the opportunity to evaluate the sign method using your own test scenarios.

Example

<script>

function display()

{

  var x=document.getElementById("num").value;

document.getElementById("result").innerHTML=Math.sign(x);

}

</script>

<form>

  Enter a number: <input type="text" id="num">

  <input type="button"  onclick="display()" value="submit">

  </form>

  <p><span id="result"></span></p>

Input Required

This code uses input(). Please provide values below: