Math.sign
Returns 1, -1, or 0 indicating the sign of a number
Syntax
Math.sign(x)Parameters
| Parameter | Type | Description |
|---|---|---|
| x | number | A number |
Return Value
1 if positive, -1 if negative, 0 if zero
Examples
console.log(Math.sign(5)); // 1
console.log(Math.sign(-5)); // -1
console.log(Math.sign(0)); // 0const values = [-10, 0, 5, -3, 8];
const signs = values.map(Math.sign);
console.log(signs); // [-1, 0, 1, -1, 1]function direction(velocity: number) {
switch (Math.sign(velocity)) {
case 1: return 'forward';
case -1: return 'backward';
default: return 'stopped';
}
}Understanding Math.sign
The Math.sign method in JavaScript returns 1, -1, or 0 indicating the sign of a number. It belongs to the Math object and is one of the most widely used methods for working with math values in modern JavaScript and TypeScript applications.
The method signature is Math.sign(x). It accepts 1 parameter: x. When called, it returns 1 if positive, -1 if negative, 0 if zero. Understanding when and how to use sign() helps you write more expressive, readable code.
Common use cases for Math.sign include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like math-abs, math-floor, math-ceil, enabling you to chain operations together for complex data manipulation pipelines.
Browser support for Math.sign is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.
Browser Compatibility
Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.
Related Methods
More Math Methods
Other methods in the Math object
Related Tools
More Math Methods
Explore JavaScript Methods
Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.