JavaScript Math.sign()
Syntax & Examples


Math.sign() static-method

The Math.sign() static method in JavaScript returns the sign of a number, indicating whether the number is positive, negative, or zero.


Syntax of Math.sign()

The syntax of Math.sign() static-method is:

Math.sign(x)

This sign() static-method of Math returns the sign of the given number x, indicating whether x is positive, negative, or zero.

Parameters

ParameterOptional/RequiredDescription
xrequiredA number whose sign is to be determined. Can be positive, negative, or zero.

Return Type

Math.sign() returns value of type Number.



✐ Examples

1 Using Math.sign() with a positive number

In JavaScript, we can use the Math.sign() method to determine the sign of a positive number.

For example,

  1. We pass the positive number 3 to the Math.sign() method.
  2. The result, 1, indicates that the number is positive.
  3. We log the result to the console using the console.log() method.

JavaScript Program

const result = Math.sign(3);
console.log(result);

Output

1

2 Using Math.sign() with a negative number

In JavaScript, we can use the Math.sign() method to determine the sign of a negative number.

For example,

  1. We pass the negative number -7 to the Math.sign() method.
  2. The result, -1, indicates that the number is negative.
  3. We log the result to the console using the console.log() method.

JavaScript Program

const result = Math.sign(-7);
console.log(result);

Output

-1

3 Using Math.sign() with zero

In JavaScript, we can use the Math.sign() method to determine the sign of zero.

For example,

  1. We pass the number 0 to the Math.sign() method.
  2. The result, 0, indicates that the number is zero.
  3. We log the result to the console using the console.log() method.

JavaScript Program

const result = Math.sign(0);
console.log(result);

Output

0

Summary

In this JavaScript tutorial, we learned about sign() static-method of Math: the syntax and few working examples with output and detailed explanation for each example.