Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

Math.sign

Returns the sign of a number: 1, -1 or 0.

Available since: ES2015

Syntax

Math.sign(x)

Parameters

ParameterTypeDescription
xnumberAny number.

Return value

number1 for a positive number, -1 for a negative one, 0 for zero.

Examples

The sign of a number:

console.log(Math.sign(42), Math.sign(-3), Math.sign(0));

Output:

1 -1 0

A label for a price change:

const change = -12;
const labels = { 1: 'up', '-1': 'down', 0: 'same' };
 
console.log(labels[Math.sign(change)]);

Output:

down

Notes

For NaN the result is NaN too.

See also

  • Math.abs — Returns the absolute value of a number — its value without the sign.

In the tutorial

  • Numbers — Numbers in JavaScript — integers and fractions, rounding, the Math object, toFixed and the special values Infinity and NaN.

Thanks for reading!

Donate to the site — we work hard for you.