JavaScript

# JavaScript Math.sign() method | Explained

When a user inputs a number on a web page application, it is frequently necessary to validate if the number is positive, negative, or something else. The built-in “Math.sign()” method in JavaScript can be utilized to find out the sign of a number at runtime. It simplifies data parsing and the process to determine the sign of a number on the client-side. As the “sign()” is a static method of the “Math” class, it can be accessed directly by invoking it with the name “Math.sign()”.

This write-up will discuss the usage of the Math.sign() method in JavaScript. So, let’s start!

## JavaScript Math.sign() method

With the ES6 JavaScript standard, determining the sign of a number is now a breeze. The “Math.sign()” method is declared in the Math class which provides access to it directly using the class name. Depending on the variables’ value, this method returns “0”, “-0”, “1”, “-1”. Also, when a “non-numeric” value is passed to the “Math.sign()” method, it returns “NaN(Not a Number).

Syntax

Math.sign(number)

Here, the “Math.sign()” method accepts the “number” as an argument and returns a value that represents its sign.

Valid values arguments for Math.sign() method: Numeric string, Floating-point number, Integer

Invalid values arguments for Math.sign() method: Empty variable, Non-numeric string.

At this point, you may wonder why to use the “Math.sign()” method when the JavaScript Comparison operators such as “>” or “<” can assist in determining if the number is positive or negative.

The below-given section will answer the stated question!

## JavaScript Math.sign() method vs Comparison Operators

A Comparison operator can be utilized when you only want to check the boolean status of a number. For instance, in the below-given example, we will validate if the value of the constant “number” is greater than “0” or not, with the help of the Greater than>” Comparison operator:

const number = 8;
number > 0;

Above-program will output “true” as the specified value “8” is “positive” and greater than “0”: Whereas the “Math.sign()” method returns a “number” value that represents a “number” value that can be used to perform further mathematical calculations:

Math.sign(number); So, it is preferred to use the “Math.sign()” method over the Comparison operators when it is required to check the sign of a number, and you have to utilize the resultant value in some other operation.

Now, let’s check out some examples related to the Math.sign() method implementation in JavaScript.

Example 1

In a JavaScript program, when a positive number is passed as an argument to the “Math.sign()” method, it will return the value “1”:

For instance, we have passed “4” to the “Math.sign()” method:

console.log(Math.sign(4));

The execution of the above-given “Math.sign()” method will return “1,” which indicates that “4” is a positive number: Example 2

Another case is when the JavaScript “Math.sign()” method accepts a negative number such as “-4”, it will output the value “-1”:

console.log(Math.sign(-4));

The returned value signifies that the passed number is “negative”: Example 3

If you have passed a “non-numeric” value to the “Math.sign()” method, then it will return “NaN” (Not a Number):

console.log(Math.sign('linuxhint'));

As in the above statement, a “linuxhint” string is passed, so the resultant case of the “Math.sign()” method will be set to “NaN”: Example 4

Passing positive zero as an argument to the “Math.sign()” method will print out “0” value:

console.log(Math.sign(0));

Output Example 5

While for a negative zero argument, the return case of the JavaScript “Math.sign()” method is set to “-0”:

console.log(Math.sign(-0));

Output After checking the above-given output, have you thought about why we need a negative zero?

JavaScript developers employ the “magnitude” of a value to point towards any information, such as the sign of a number representing the direction of the movement. In such applications, if the variable loses its sign, all of its information will be lost automatically. That’s why preserving the sign of zero (-0) with the “Math.sign()” method prevents us from information loss.

That was all the essential information related to the JavaScript Math.sign() method. You can further research it according to your requirements.

## Conclusion

The JavaScript Math.sign() method is used to check the sign of a number and it returns “0” for positive zero, “-0” for negative zero, “1” for a positive number, and “-1” for a negative number. Also, when a non-numeric value is passed to the Math.sign() method, it returns “NaN” (Not a Number). Math.sign method is declared in Math class, which provides access to it directly utilizing the class name. This write-up discussed the usage of the JavaScript Math.sign() method. 