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
Parameter | Optional/Required | Description |
---|---|---|
x | required | A 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,
- We pass the positive number
3
to theMath.sign()
method. - The result,
1
, indicates that the number is positive. - 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,
- We pass the negative number
-7
to theMath.sign()
method. - The result,
-1
, indicates that the number is negative. - 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,
- We pass the number
0
to theMath.sign()
method. - The result,
0
, indicates that the number is zero. - 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.