How to Check if a Number is Positive or Negative in JavaScript Using Math.sign()

← PrevNext →

Last updated: 27th June 2025

In JavaScript, determining whether a number is positive or negative can be done using two straightforward methods. The first involves using comparison operators (e.g., >, <, or ===) to evaluate the number's sign. The second leverages the built-in ES6 Math.sign() function, which returns 1, -1, or 0 based on the input value. Both methods provide quick and effective ways to check a number’s sign in JavaScript.

Let’s see an example using both the methods.

1) Using Comparison Operator

<script>
  const number = -5;

  const message = number < 0 
    ? `${number} is negative` 
    : `${number} is positive`;

    document.write(message);
</script>
Try it

In the example above, the < (less than) operator is used to determine whether the number is negative or positive.

While it's obvious that the assigned variable holds a negative value in this (above) example, such visual cues aren't available during complex calculations. In real-world scenarios, especially at runtime, implementing a condition like the one shown above becomes essential to accurately determine whether a number is negative or positive.

Now, guess what the above condition returns. Well, it returns a Boolean true or false. Do this to check…

<script>
  const number = -5;
  document.write (number < 0);
</script>

The output is true, since it’s a negative value.

2) Using Math.sign()

In JavaScript, you can use the ES6 Math.sign() function to determine whether a number is positive, negative, or zero. This method accepts a single numeric argument and returns 1, -1, 0, or -0 based on the value provided.

Here's the syntax:

Math.sign(number)

The function will return either of this …
1, -1, 0, -0, or NaN

The Math.sign() function returns the sign of a given number, rather than its actual value. It returns 1 for positive numbers, -1 for negative numbers, and 0 for a value of zero.

NaN denotes "Not a Number", in case the parameter (or the argument) is not a number.

Remember, the function is "case sensitive".

Now, let's see how the function works.

<script>
  const number = 17;

    let result;
    if (Math.sign(number) === 1) {
        result = "is positive";
    } else if (Math.sign(number) === -1) {
        result = "is negative";
    } else {
        result = "is zero";
    }

    document.write(`${number} ${result}`);
</script>
Try it

Difference between Comparison Operator and Math.sign() function

The key difference between using a "comparison operator" and the "Math.sign()" function in JavaScript lies in their return values. A comparison operator returns a Boolean value (true or false), whereas Math.sign() returns the sign of the number. 1 for positive, -1 for negative, and 0 for zero. Let's explore both methods side by side to see how they work in practice.

<script>
    const number = -7;

    // Method 1: Using Comparison Operator.
    let result1;
    if (number < 0) {
        result1 = "negative (comparison)";
    } else if (number > 0) {
        result1 = "positive (comparison)";
    } else {
        result1 = "zero (comparison)";
    }

    // Method 2: Using Math.sign().
    let result2;
    switch (Math.sign(number)) {
        case 1:
            result2 = "positive (Math.sign)";
            break;
        case -1:
            result2 = "negative (Math.sign)";
            break;
        case 0:
        case -0: // handles negative zero.
            result2 = "zero (Math.sign)";
            break;
    }

    document.write(`Using comparison: ${number} is ${result1}<br>`);
    document.write(`Using Math.sign(): ${number} is ${result2}`);
</script>

The above example demonstrates how both approaches deliver the same result but differ in syntax and return types, Boolean logic for "comparisons", numeric indicators for "Math.sign()".

Examples using Math.sign()

Example
1. document.write (Math.sign()); Try it
2. const number = -5 - 10;
document.write (Math.sign(number));Try it
3. const number = -15 + 12;
document.write (Math.sign(number)); Try it

← PreviousNext →