Reference Materials
Certification Courses
Created with over a decade of experience and thousands of feedback.
JavaScript Math sign()
In this tutorial, you are going to learn about the JavaScript Math.sign() method with the help of examples.
The sign() method computes the sign of the specified number and returns 1 if the number is positive and -1 if it's negative.
Example
// sign() with a positive number
let value = Math.sign(126);
console.log(value);
// Output: 1
sign() Syntax
The syntax of the Math.sign() method is:
Math.sign(number)
Here, sign() is a static method. Hence, we are accessing the method using the class name, Math.
sign() Parameter
The Math.sign() method takes a single parameter:
number- value whose sign is to be determined
sign() Return Value
The sign() method returns:
- 1 if the argument is positive
- -1 if the argument is negative
- NaN (Not a Number) for a non-numeric argument
Example 1: JavaScript Math.sign()
// sign() with negative argument
let value1 = Math.sign(-27);
console.log(value1);
// sign() with positive argument
let value2 = Math.sign(16);
console.log(value2);
// Output:
// -1
// 1
Here, the Math.sign() returns
- -1 - for the negative number
-27 - 1 - for the positive number
16
Example 2: Math.sign() with Non-Numeric Arguments
let string = "Harry";
// sign() with non-numeric argument
let result = Math.sign(string);
console.log(result);
// Output: NaN
In the above example, we have used the Math.sign() method with a string value "Harry". Hence, we get NaN as output.
Example 3: Math.sign() with Zero Values
// sign() with negative zero
let value1 = Math.sign(-0);
console.log(value1);
// Output: -0
// sign() with positive zero
let value2 = Math.sign(0);
console.log(value2);
// Output: 0
Also Read:
Did you find this article helpful?