References

Beginner-friendly references for web development, with live, editable examples.

The JavaScript Math.sign() method

Method JavaScript All modern browsers Updated
Quick answer

The Math.sign() method returns the sign of a number as a simple value: -1 for negatives, 1 for positives, and 0 (or -0) for zero. Math.sign(-42) is -1. It's handy for determining direction without caring about magnitude. Non-numeric input returns NaN.

Overview

Math.sign() tells you whether a number is positive, negative or zero, reduced to a tidy 1, -1 or 0. It strips away the magnitude and keeps only the direction, which is exactly what you want for movement and comparison logic.

The classic uses are about direction: Math.sign(target - current) tells you which way to move (and you can multiply a step by it), and it's a neat building block for comparison functions or for normalizing a delta to -1/0/1. It pairs naturally with Math.abs(), which gives the magnitude while sign gives the direction.

The edge cases: it returns 0 for +0 and -0 for -0 (a distinction that rarely matters), and NaN for values that aren't numbers. For a value you can compute its sign and magnitude separately and recombine — Math.sign(x) * Math.abs(x) is just x again.

Syntax

Math.sign(-42)   // -1
Math.sign(42)    // 1
Math.sign(0)     // 0
Math.sign("x")   // NaN

Parameters

The Math.sign() method accepts the following parameters.

Parameter Description
x The number whose sign you want (-1, 0, or 1).

Example

Live example
<pre id="out" style="font:15px ui-monospace,monospace"></pre>
<script>
  function step(current, target) {
    return current + Math.sign(target - current); // move one toward target
  }

  document.getElementById('out').textContent =
    'from 3 toward 10: ' + step(3, 10) + '\n' +
    'from 8 toward 2:  ' + step(8, 2); // 4 / 7
</script>

Best practices

  • Use it to get direction without magnitude — e.g. which way to move toward a target.
  • Pair it with Math.abs() (direction vs magnitude).
  • Multiply a step by Math.sign(delta) to move the right way by a fixed amount.
  • Validate input — non-numbers return NaN.

Frequently asked questions

What does Math.sign() return?
-1 for negative numbers, 1 for positive, and 0 (or -0) for zero. Non-numbers return NaN.
How do I check if a number is positive or negative?
Use Math.sign(n): a result of 1 means positive, -1 negative, 0 zero.
How is Math.sign() different from Math.abs()?
Math.sign() gives the direction (sign); Math.abs() gives the magnitude (always non-negative).
What does Math.sign() return for 0?
0 for positive zero and -0 for negative zero — a distinction that rarely matters in practice.